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

#ifdef LOCAL
template<class T, class U>
ostream& operator<<(ostream& os, pair<T, U> p) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template<class C, class = typename C::value_type>
typename enable_if<!is_same<C, string>::value, ostream&>::type 
operator<<(ostream& os, C c) {
	for (auto i = c.begin(); i != c.end(); i++) {
		os << " {"[i == c.begin()] << *i << ",}"[next(i) == c.end()];
	}
	return os;
}

#define debug(x) { cerr << #x << " = " << x << endl; }
#else
#define debug(...) {}
#endif

map<int, int> paths;
map<int, int> result;

const int N = 2000;

bool dis[N];

pair<int,pair<vector<int>, vector<int>>> dynamic(vector<vector<int>>& G) {
	int n = G.size() - 1;
	vector<int> dp(n + 1, 1);
	vector<int> from(n + 1);
	int MAX = 0;
	for (int i = 1; i <= n; i ++) {
		if (dis[i]) {
			dp[i] = 0;
			continue;
		}
		for (int y : G[i]) {
			if (dp[y] < dp[i] + 1) {
				from[y] = i;
				dp[y] = dp[i] + 1;
			}
		}
		MAX = max(MAX, dp[i]);
	}
	return {MAX, {dp, from}};
}

int backtrack(vector<vector<int>>& G, int k) {
	auto res = dynamic(G);
	if (k == 0) {
		return res.first;
	}
	int n = G.size() - 1;
	int MAX = res.first;
	int MIN = n;
	for (int i = 1; i <= n; i ++) {
		if (res.second.first[i] == MAX) {
			int x = i;
			while (x != 0) {
				if (not dis[i]) {
					dis[x] = true;
					MIN = min(MIN, backtrack(G, k - 1));
					dis[x] = false;
				}
				x = res.second.second[x];
			}
			return MIN;
		}
	}
	return -1;
}

vector<vector<int>> read_graph(int n, int m) {
	vector<vector<int>> G(n + 1);
	for (int i = 0; i < m; i ++) {
		int a, b;
		cin >> a >> b;
		G[a].push_back(b);
	}
	return G;
}

int main() {
	ios_base::sync_with_stdio(false);
	int n, m, k;
	cin >> n >> m >> k;
	auto g = read_graph(n, m);
	cout << backtrack(g, k) << "\n";
}