1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <bits/stdc++.h>

using namespace std;

const int N = 302;
int n, m, k;
vector<int> g[N];
vector<int> gin[N];

int dists[N];
void dfs(int v) {
    int len = 1;
    for (int next : g[v]) {
        if (dists[next] == -1) {
            dfs(next);
        }
        len = max(len, dists[next] + 1);
    }
    dists[v] = len;
}

void update_dists(const set<int> & bans) {
    memset(dists, 255, sizeof(dists));
    for (int v : bans) {
        dists[v] = 0;
    }
    for (int i = 0; i < n; i++) {
        if (dists[i] == -1) {
            dfs(i);
        }
    }
}

int get_longest() {
    int best = 0;
    for (int i = 0; i < n; i++) {
        best = max(best, dists[i]);
    }
    return best;
}

int added[N];
void add_vertex(int v, int required, vector<int> & cands) {
    added[v] = 1;
    cands.push_back(v);
    for (int next : g[v]) {
        if (dists[next] == required - 1) {
            if (added[next] == 0) {
                add_vertex(next, required - 1, cands);
            }
        }
    }
}

void get_cands(vector<int> & cands, int best) {
    memset(added, 0, sizeof(added));
    for(int i = 0; i < n; i++) {
        if (dists[i] == best) {
            add_vertex(i, best, cands);
        }
    }
}

unordered_set<long long> mem;

int answer;
int biggest_cands = 0;
vector<int> vcands[5];
int cnt = 0;
void try_next_ban(set<int> & bans) {
    if (answer <= 1) {
        return;
    }
    long long h = 0;
    for (int x : bans) {
        h <<= 16;
        h += (x+1);
    }

    if (mem.find(h) != mem.end()) {
        return;
    }
    mem.insert(h);
    cnt++;

    update_dists(bans);
    int lp = get_longest();

    if (bans.size() == k) {
        if (lp < answer) {
            // cerr << "updated " << lp << " on " << cnt << endl;
            answer = lp;
        }
        return;
    }

    vector<int> & cands = vcands[bans.size()];
    cands.clear();
    get_cands(cands, lp);

    // printf("longest path: %d cands size: %d\n", lp, (int)cands.size());
    biggest_cands = max(biggest_cands, (int)cands.size());

    for (int x : cands) {
        if (bans.insert(x).second) {
            try_next_ban(bans);
            bans.erase(x);
        }
    }
}

void solve() {
    if (k == n) {
        printf("0\n");
        return;
    }
    answer = n;
    set<int> s;
    try_next_ban(s);
    printf("%d\n", answer);
}

void read() {
    scanf("%d%d%d", &n, &m, &k);
    for (int j = 0; j < m; j++) {
        int a, b;
        scanf("%d%d", &a, &b);
        a--;
        b--;
        g[a].push_back(b);
        gin[b].push_back(a);
    }
}

void gen() {
    n = 300;
    m = 400;
    k = 4;

    n = min(300, 1 + rand() % 400);
    m = min(400, 1 + rand() % 600);
    int mxm = n * (n-1) / 2;
    m = min(m, mxm);

    for (int i = 0; i < N; i++) {
        g[i].clear();
        gin[i].clear();
    }

    srand(time(0)+clock());
    set<pair<int, int> > s;
    for (int j = 0; j < m;) {
        int a = rand() % n;
        int b = rand() % n;
        if (a < b) {
            if (s.find({a,b}) == s.end()) {
                // cout << a << "->" << b << endl;
                s.insert({a,b});
                g[a].push_back(b);
                gin[b].push_back(a);
                j++;
            }
        }
    }
}

void write_graph() {
    fstream fs;
    fs.open ("last.in", std::fstream::out | std::ofstream::trunc);
    fs << n << " " << m << " " << k << endl;
    for (int i = 0; i < n; i++) {
        for (int j : g[i]) {
            fs << (i+1) << " " << (j+1) << endl;
        }
    }
    fs.close();
}

void normal() {
    mem.reserve(8000000);
    read();
    solve();
    exit(0);
}

void rad() {
    long long w = 0;
    long long mod = 1e9+7;
    long long w100 = 1;
    for (int i = 1; i <= 1360; i++) {
        for (int i = 0; i < N; i++) {
            g[i].clear();
            gin[i].clear();
        }
        stringstream ss;
        ss << "testy/radeusz";
        ss << i;
        ss << ".in";
        string s;
        ss >> s;
        freopen(s.c_str(), "r", stdin);
        read();
        mem.clear();
        solve();
        long long x = answer;
        w100 *= 100;
        w100 %= mod;
        w = (w + x * w100) % mod;
    }
    cout << w << endl;
    exit(0);
}

int main() {
    normal();
    //rad();

    //read();
    int mxmem = 0;
    for (int i = 0; i < 1e9; i++) {
        gen();
        mem.clear();
        cnt = 0;
        solve();
        printf("biggest cands: %d\n", biggest_cands);
        int currmem = (int)mem.size();
        if (currmem > mxmem) {
            mxmem = currmem;
            write_graph();
        }
        printf("MEM: %d %d\n", mxmem, currmem);
    }

    return 0;
}