#include <iostream> #include <vector> using namespace std; const int MAXN = 305, inf = 1e9; int res = inf, dp[MAXN], k, n, num = 1, vis[MAXN]; vector<int> G[MAXN]; void dfs(int v) { dp[v] = 1, vis[v] = num; for (int u : G[v]) { if (vis[u] < num) { dfs(u); } dp[v] = max(dp[v], dp[u] + 1); } } int bads[5]; void rek(int j) { if (j > k) { num++; for (int i = 1; i <= k; i++) { vis[bads[i]] = num; dp[bads[i]] = 0; } dfs(0); res = min(res, dp[0] - 1); return; } for (int i = bads[j - 1] + 1; i <= n; i++) { bads[j] = i; rek(j + 1); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int m; cin >> n >> m >> k; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; G[a].push_back(b); } for (int i = 1; i <= n; i++) { G[0].push_back(i); } rek(1); cout << res << "\n"; }
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 | #include <iostream> #include <vector> using namespace std; const int MAXN = 305, inf = 1e9; int res = inf, dp[MAXN], k, n, num = 1, vis[MAXN]; vector<int> G[MAXN]; void dfs(int v) { dp[v] = 1, vis[v] = num; for (int u : G[v]) { if (vis[u] < num) { dfs(u); } dp[v] = max(dp[v], dp[u] + 1); } } int bads[5]; void rek(int j) { if (j > k) { num++; for (int i = 1; i <= k; i++) { vis[bads[i]] = num; dp[bads[i]] = 0; } dfs(0); res = min(res, dp[0] - 1); return; } for (int i = bads[j - 1] + 1; i <= n; i++) { bads[j] = i; rek(j + 1); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int m; cin >> n >> m >> k; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; G[a].push_back(b); } for (int i = 1; i <= n; i++) { G[0].push_back(i); } rek(1); cout << res << "\n"; } |