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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef vector<vector<int>> Graph;

void rec(int poz, int k, Graph &g, Graph &h, vector<bool> &used, int &ans) {
    int n = g.size();
    vector<int> left(n, 0), right(n, 0);
    int longest = 0;

    for(int i = n - 1; i >= 0; i -= 1) {
        if(used[i])
            continue;
        int best = 0;
        for(auto v : g[i]) {
            if(not used[v])
                best = max(best, right[v]);
        }   
        right[i] = best + 1;
        longest = max(longest, right[i]);
    }

    if(poz == k) {
        ans = min(ans, longest);
        return;
    }

    for(int i = 0; i < n; i += 1) {
        if(used[i])
            continue;
        int best = 0;
        for(auto v : h[i]) {
            if(not used[v])
                best = max(best, left[v]);
        }
        left[i] = best + 1;
    }

    for(int i = 0; i < n; i += 1) {
        if(not used[i] and left[i] + right[i] - 1 == longest) {
            used[i] = 1;
            rec(poz + 1, k, g, h, used, ans);
            used[i] = 0;
        }
    }
};

int main() {
    int n, m, k; cin >> n >> m >> k;
    
    Graph g(n), h(n);

    for(int i = 0; i < m; i += 1) {
        int a, b; cin >> a >> b;
        a -= 1; 
        b -= 1;
        g[a].push_back(b);
        h[b].push_back(a);
    }

    vector<bool> used(n, 0);
    int best = 1e9;

    rec(0, k, g, h, used, best);

    cout << best << "\n";
}