#include <bits/stdc++.h> using namespace std; typedef long long int LL; const int N = 307; const int MAXPOSSIBLE = 1e6; int n, m, k; int ans = N; bool block[N]; int Length[N]; vector <int> G[N]; int Count(int a, int b){ LL ret = 1LL; while(b--) ret *= a; if(ret > MAXPOSSIBLE) return MAXPOSSIBLE + 1; return ret; } int calc(){ int ret = 0; for(int i = n; i >= 1; --i){ if(block[i]) continue; Length[i] = 1; for(int v: G[i]) if(!block[v]) Length[i] = max(Length[i], Length[v] + 1); ret = max(ret, Length[i]); } return ret; } void go(int Left){ if(Left == 0){ ans = min(ans, calc()); return; } int t = calc(); int Start = -1; for(int i = 1; i <= n; ++i) if(!block[i] && Length[i] == t) Start = i; vector <int> maybe; maybe.push_back(Start); while(Length[Start] > 1){ for(int v: G[Start]) if(!block[v] && Length[v] + 1 == Length[Start]){ Start = v; break; } maybe.push_back(Start); } for(int v: maybe){ block[v] = true; go(Left - 1); block[v] = false; } } int main(){ scanf("%d %d %d", &n, &m, &k); for(int i = 0; i < m; ++i){ int u, v; scanf("%d %d", &u, &v); G[u].push_back(v); } if(n <= k){ puts("0"); return 0; } if(m <= k){ puts("1"); return 0; } go(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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #include <bits/stdc++.h> using namespace std; typedef long long int LL; const int N = 307; const int MAXPOSSIBLE = 1e6; int n, m, k; int ans = N; bool block[N]; int Length[N]; vector <int> G[N]; int Count(int a, int b){ LL ret = 1LL; while(b--) ret *= a; if(ret > MAXPOSSIBLE) return MAXPOSSIBLE + 1; return ret; } int calc(){ int ret = 0; for(int i = n; i >= 1; --i){ if(block[i]) continue; Length[i] = 1; for(int v: G[i]) if(!block[v]) Length[i] = max(Length[i], Length[v] + 1); ret = max(ret, Length[i]); } return ret; } void go(int Left){ if(Left == 0){ ans = min(ans, calc()); return; } int t = calc(); int Start = -1; for(int i = 1; i <= n; ++i) if(!block[i] && Length[i] == t) Start = i; vector <int> maybe; maybe.push_back(Start); while(Length[Start] > 1){ for(int v: G[Start]) if(!block[v] && Length[v] + 1 == Length[Start]){ Start = v; break; } maybe.push_back(Start); } for(int v: maybe){ block[v] = true; go(Left - 1); block[v] = false; } } int main(){ scanf("%d %d %d", &n, &m, &k); for(int i = 0; i < m; ++i){ int u, v; scanf("%d %d", &u, &v); G[u].push_back(v); } if(n <= k){ puts("0"); return 0; } if(m <= k){ puts("1"); return 0; } go(k); printf("%d\n", ans); return 0; } |