#include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<b;++i) #define FOROP(i,a,b,op) for(int i=a;i<b;op) #define FORD(i,a,b) for(int i=a;i>=b;--i) #define PB push_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define pii pair<int, int> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; #pragma optimize GCC ("Ofast") int forbiden[5]; int n, m, k; int sw[310]; int swcp[310]; int d[310]; vvi V(310); int compute(){ FOR(i,0,k){ for(int x : V[forbiden[i]]){ sw[x]--; } sw[forbiden[i]] = 494; } queue<int> q; int idx = 0; FOR(i,0,n){ if (idx < k && forbiden[idx] == i){ idx++; continue; } if (sw[i] == 0){ q.push(i); d[i] = 1; } } while(!q.empty()){ int t = q.front(); q.pop(); for(int x : V[t]){ sw[x]--; d[x] = d[t] + 1; if (sw[x] == 0) q.push(x); } } int ans = 0; idx = 0; FOR(i,0,n){ sw[i] = swcp[i]; if (idx < k && forbiden[idx] == i){ idx++; continue; } ans = max(ans, d[i]); } return ans; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> k; int p, q; FOR(i,0,310) sw[i] = 0; FOR(i,0,m){ cin >> p >> q; p--; q--; sw[q]++; V[p].PB(q); } FOR(i,0,n) swcp[i] = sw[i]; int ans = 1000000; if (k == 0){ ans = compute(); } else if (k == 1){ FOR(i,0,n){ forbiden[0] = i; ans = min(ans, compute()); } } else if (k == 2){ FOR(i,0,n){ forbiden[0] = i; FOR(j,i+1,n){ forbiden[1] = j; ans = min(ans, compute()); } } } else if (k == 3){ FOR(i,0,n){ forbiden[0] = i; FOR(j,i+1,n){ forbiden[1] = j; FOR(l,j+1,n){ forbiden[2] = l; ans = min(ans, compute()); } } } } else if (k == 4){ FOR(i,0,n){ forbiden[0] = i; FOR(j,i+1,n){ forbiden[1] = j; FOR(l,j+1,n){ forbiden[2] = l; FOR(s,l+1,n) { forbiden[3] = s; ans = min(ans, compute()); } } } } } cout << ans << endl; }
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | #include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<b;++i) #define FOROP(i,a,b,op) for(int i=a;i<b;op) #define FORD(i,a,b) for(int i=a;i>=b;--i) #define PB push_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define pii pair<int, int> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; #pragma optimize GCC ("Ofast") int forbiden[5]; int n, m, k; int sw[310]; int swcp[310]; int d[310]; vvi V(310); int compute(){ FOR(i,0,k){ for(int x : V[forbiden[i]]){ sw[x]--; } sw[forbiden[i]] = 494; } queue<int> q; int idx = 0; FOR(i,0,n){ if (idx < k && forbiden[idx] == i){ idx++; continue; } if (sw[i] == 0){ q.push(i); d[i] = 1; } } while(!q.empty()){ int t = q.front(); q.pop(); for(int x : V[t]){ sw[x]--; d[x] = d[t] + 1; if (sw[x] == 0) q.push(x); } } int ans = 0; idx = 0; FOR(i,0,n){ sw[i] = swcp[i]; if (idx < k && forbiden[idx] == i){ idx++; continue; } ans = max(ans, d[i]); } return ans; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> k; int p, q; FOR(i,0,310) sw[i] = 0; FOR(i,0,m){ cin >> p >> q; p--; q--; sw[q]++; V[p].PB(q); } FOR(i,0,n) swcp[i] = sw[i]; int ans = 1000000; if (k == 0){ ans = compute(); } else if (k == 1){ FOR(i,0,n){ forbiden[0] = i; ans = min(ans, compute()); } } else if (k == 2){ FOR(i,0,n){ forbiden[0] = i; FOR(j,i+1,n){ forbiden[1] = j; ans = min(ans, compute()); } } } else if (k == 3){ FOR(i,0,n){ forbiden[0] = i; FOR(j,i+1,n){ forbiden[1] = j; FOR(l,j+1,n){ forbiden[2] = l; ans = min(ans, compute()); } } } } else if (k == 4){ FOR(i,0,n){ forbiden[0] = i; FOR(j,i+1,n){ forbiden[1] = j; FOR(l,j+1,n){ forbiden[2] = l; FOR(s,l+1,n) { forbiden[3] = s; ans = min(ans, compute()); } } } } } cout << ans << endl; } |