#include<bits/stdc++.h> #define mp make_pair #define pb push_back #define st first #define nd second #define N 304 using namespace std; int n, m, k; int ans[N], res = 1e9; vector<int> G[N], S[5]; bool vis[N], block[N]; void DFS(int v) { vis[v] = true; for(auto u : G[v]) { if(!vis[u] && !block[u]) DFS(u); if(!block[u]) ans[v] = max(ans[v], ans[u] + 1); } } pair<int,int> findBest() { pair<int,int> best = mp(0,-1); for(int i = 1 ; i <= n ; ++i) { if(!vis[i] && !block[i]) DFS(i); if(best.nd < ans[i]) { best.nd = ans[i]; best.st = i; } } return best; } void checkAns() { fill(vis, vis + n + 1, false); fill(ans, ans + n + 1, 0); pair<int,int> best = findBest(); res = min(res, best.nd); } void search(int l) { fill(vis, vis + n + 1, false); fill(ans, ans + n + 1, 0); pair<int,int> best = findBest(); S[l].clear(); while(best.nd > 0) { S[l].pb(best.st); for(auto u : G[best.st]) { if(ans[u] == best.nd - 1 && !block[u]) { best.st = u; best.nd = ans[u]; break; } } } S[l].pb(best.st); } void backTrack(int l) { if(l > k) { checkAns(); return; } search(l); for(auto v : S[l]) { if(block[v] == true) continue; block[v] = true; backTrack(l + 1); block[v] = false; } } void init() { scanf("%d%d%d", &n, &m, &k); for(int i = 0 ; i < m ; ++i) { int a, b; scanf("%d%d", &a, &b); G[a].pb(b); } } int main() { init(); backTrack(1); if(res) printf("%d\n", res+1); else puts("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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include<bits/stdc++.h> #define mp make_pair #define pb push_back #define st first #define nd second #define N 304 using namespace std; int n, m, k; int ans[N], res = 1e9; vector<int> G[N], S[5]; bool vis[N], block[N]; void DFS(int v) { vis[v] = true; for(auto u : G[v]) { if(!vis[u] && !block[u]) DFS(u); if(!block[u]) ans[v] = max(ans[v], ans[u] + 1); } } pair<int,int> findBest() { pair<int,int> best = mp(0,-1); for(int i = 1 ; i <= n ; ++i) { if(!vis[i] && !block[i]) DFS(i); if(best.nd < ans[i]) { best.nd = ans[i]; best.st = i; } } return best; } void checkAns() { fill(vis, vis + n + 1, false); fill(ans, ans + n + 1, 0); pair<int,int> best = findBest(); res = min(res, best.nd); } void search(int l) { fill(vis, vis + n + 1, false); fill(ans, ans + n + 1, 0); pair<int,int> best = findBest(); S[l].clear(); while(best.nd > 0) { S[l].pb(best.st); for(auto u : G[best.st]) { if(ans[u] == best.nd - 1 && !block[u]) { best.st = u; best.nd = ans[u]; break; } } } S[l].pb(best.st); } void backTrack(int l) { if(l > k) { checkAns(); return; } search(l); for(auto v : S[l]) { if(block[v] == true) continue; block[v] = true; backTrack(l + 1); block[v] = false; } } void init() { scanf("%d%d%d", &n, &m, &k); for(int i = 0 ; i < m ; ++i) { int a, b; scanf("%d%d", &a, &b); G[a].pb(b); } } int main() { init(); backTrack(1); if(res) printf("%d\n", res+1); else puts("0"); } |