#pragma GCC optimize ("unroll-loops") #include <bits/stdc++.h> #define mp make_pair #define fi first #define se second #define pb push_back #define all(x) (x).begin(),(x).end() using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector < int > VI; typedef double D; const int MN = 100005, inf = 1000000005, mod = 1000000007; const LL INF = 1000000000000000005LL; int n, m; int ile[MN], wyw[MN], per[MN]; VI G[MN]; inline int licz() { int ret = 0; for(int i = 1; i <= n; ++i) { ile[i] = 0; if(wyw[i]) continue; for(auto v : G[i]) ile[i] = max(ile[i], ile[v]); ++ile[i]; ret = max(ret, ile[i]); } return ret; } int solve1() { int ans = inf; for(int i = 1; i <= n; ++i) { wyw[i] = 1; ans = min(ans, licz()); wyw[i] = 0; } return ans; } int solve2() { int ans = inf; for(int i = 1; i <= n; ++i) { wyw[i] = 1; for(int j = i + 1; j <= n; ++j) { wyw[j] = 1; ans = min(ans, licz()); wyw[j] = 0; } wyw[i] = 0; } return ans; } int solve3() { int ans = inf; for(int i = 1; i <= n; ++i) { wyw[per[i]] = 1; for(int j = i + 1; j <= n; ++j) { wyw[per[j]] = 1; for(int k = j + 1; k <= n; ++k) { wyw[per[k]] = 1; ans = min(ans, licz()); wyw[per[k]] = 0; } if(clock() > 1.9 * CLOCKS_PER_SEC) return ans; wyw[per[j]] = 0; } wyw[per[i]] = 0; } return ans; } int solve4() { int ans = inf; for(int i = 1; i <= n; ++i) { wyw[per[i]] = 1; for(int j = i + 1; j <= n; ++j) { wyw[per[j]] = 1; for(int k = j + 1; k <= n; ++k) { wyw[per[k]] = 1; for(int l = k + 1; l <= n; ++l) { wyw[per[l]] = 1; ans = min(ans, licz()); wyw[per[l]] = 0; } wyw[per[k]] = 0; } if(clock() > 1.9 * CLOCKS_PER_SEC) return ans; wyw[per[j]] = 0; } wyw[per[i]] = 0; } return ans; } int main() { int *a = new int; srand((LL)a); int k; scanf("%d%d%d", &n, &m, &k); for(int i = 1; i <= m; ++i) { int a, b; scanf("%d%d", &a, &b); G[b].pb(a); } for(int i = 1; i <= n; ++i) per[i] = i; random_shuffle(per + 1, per + 1 + n); int ans = inf; if(k >= n) { printf("0"); return 0; } if(k == 0) ans = licz(); if(k == 1) ans = solve1(); if(k == 2) ans = solve2(); if(k == 3) ans = solve3(); if(k == 4) ans = solve4(); printf("%d", ans); }
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | #pragma GCC optimize ("unroll-loops") #include <bits/stdc++.h> #define mp make_pair #define fi first #define se second #define pb push_back #define all(x) (x).begin(),(x).end() using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector < int > VI; typedef double D; const int MN = 100005, inf = 1000000005, mod = 1000000007; const LL INF = 1000000000000000005LL; int n, m; int ile[MN], wyw[MN], per[MN]; VI G[MN]; inline int licz() { int ret = 0; for(int i = 1; i <= n; ++i) { ile[i] = 0; if(wyw[i]) continue; for(auto v : G[i]) ile[i] = max(ile[i], ile[v]); ++ile[i]; ret = max(ret, ile[i]); } return ret; } int solve1() { int ans = inf; for(int i = 1; i <= n; ++i) { wyw[i] = 1; ans = min(ans, licz()); wyw[i] = 0; } return ans; } int solve2() { int ans = inf; for(int i = 1; i <= n; ++i) { wyw[i] = 1; for(int j = i + 1; j <= n; ++j) { wyw[j] = 1; ans = min(ans, licz()); wyw[j] = 0; } wyw[i] = 0; } return ans; } int solve3() { int ans = inf; for(int i = 1; i <= n; ++i) { wyw[per[i]] = 1; for(int j = i + 1; j <= n; ++j) { wyw[per[j]] = 1; for(int k = j + 1; k <= n; ++k) { wyw[per[k]] = 1; ans = min(ans, licz()); wyw[per[k]] = 0; } if(clock() > 1.9 * CLOCKS_PER_SEC) return ans; wyw[per[j]] = 0; } wyw[per[i]] = 0; } return ans; } int solve4() { int ans = inf; for(int i = 1; i <= n; ++i) { wyw[per[i]] = 1; for(int j = i + 1; j <= n; ++j) { wyw[per[j]] = 1; for(int k = j + 1; k <= n; ++k) { wyw[per[k]] = 1; for(int l = k + 1; l <= n; ++l) { wyw[per[l]] = 1; ans = min(ans, licz()); wyw[per[l]] = 0; } wyw[per[k]] = 0; } if(clock() > 1.9 * CLOCKS_PER_SEC) return ans; wyw[per[j]] = 0; } wyw[per[i]] = 0; } return ans; } int main() { int *a = new int; srand((LL)a); int k; scanf("%d%d%d", &n, &m, &k); for(int i = 1; i <= m; ++i) { int a, b; scanf("%d%d", &a, &b); G[b].pb(a); } for(int i = 1; i <= n; ++i) per[i] = i; random_shuffle(per + 1, per + 1 + n); int ans = inf; if(k >= n) { printf("0"); return 0; } if(k == 0) ans = licz(); if(k == 1) ans = solve1(); if(k == 2) ans = solve2(); if(k == 3) ans = solve3(); if(k == 4) ans = solve4(); printf("%d", ans); } |