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
#include <bits/stdc++.h>


using namespace std;

int backtrack(int n, int k, vector <bool> &taken, vector <vector<int>> &g) {
    vector <int> len(n, 1), prv(n, -1);
    int maxLen = 0;

    for (int i = 0; i < n; i++) if (!taken[i]) {
        for (int j : g[i]) if (!taken[j] && len[j] + 1 > len[i]) {
            len[i] = len[j] + 1;
            prv[i] = j;
        }

        maxLen = max(maxLen, len[i]);
    }

    if (k == 0) {
        return maxLen;
    }

    int ans = maxLen;

    int v = find(len.begin(), len.end(), maxLen) - len.begin();
    while (v != -1) {
        taken[v] = true;
        ans = min(ans, backtrack(n, k - 1, taken, g));

        taken[v] = false;

        v = prv[v];
    }

    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);

    int n, m, k;
    cin >> n >> m >> k;

    vector <vector<int>> g(n);
    while (m--) {
        int x, y;
        cin >> x >> y; x--; y--;

        g[y].push_back(x);
    }

    vector <bool> taken(n, false);

    cout << backtrack(n, k, taken, g);

    return 0;
}