#include <bits/stdc++.h> using namespace std; const int N = 305; int n, m, k, a, b; vector<int> V[N]; bool removed[N]; int dp[N], nxt[N]; int ans; int start; int solve() { int ret = 0; for (int i = n; i >= 1; i--) { if (removed[i]) continue; dp[i] = 1; nxt[i] = n + 1; for (int u : V[i]) { if (removed[u]) { continue; } dp[i] = max(dp[i], dp[u] + 1); if (dp[i] == dp[u] + 1) { nxt[i] = u; } } ret = max(ret, dp[i]); if (dp[i] == ret) { start = i; } } return ret; } void backtrack(int r) { if (r == 0) { ans = min(ans, solve()); return; } solve(); int w = start; vector<int> path; while (w != n + 1) { path.push_back(w); w = nxt[w]; } for (int w : path) { removed[w] = true; backtrack(r - 1); removed[w] = false; } } int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d %d", &a, &b); V[a].push_back(b); } ans = solve(); backtrack(k); printf("%d\n", ans); return 0; }
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 | #include <bits/stdc++.h> using namespace std; const int N = 305; int n, m, k, a, b; vector<int> V[N]; bool removed[N]; int dp[N], nxt[N]; int ans; int start; int solve() { int ret = 0; for (int i = n; i >= 1; i--) { if (removed[i]) continue; dp[i] = 1; nxt[i] = n + 1; for (int u : V[i]) { if (removed[u]) { continue; } dp[i] = max(dp[i], dp[u] + 1); if (dp[i] == dp[u] + 1) { nxt[i] = u; } } ret = max(ret, dp[i]); if (dp[i] == ret) { start = i; } } return ret; } void backtrack(int r) { if (r == 0) { ans = min(ans, solve()); return; } solve(); int w = start; vector<int> path; while (w != n + 1) { path.push_back(w); w = nxt[w]; } for (int w : path) { removed[w] = true; backtrack(r - 1); removed[w] = false; } } int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d %d", &a, &b); V[a].push_back(b); } ans = solve(); backtrack(k); printf("%d\n", ans); return 0; } |