#include <bits/stdc++.h> #define FOR(i, a, b) for (int i=(a); (i)<(b); (i)++) #define PPC(x) __builtin_popcountll((x)) #define ALL(x) (x).begin(), (x).end() #define pb push_back using namespace std; const int maxN = 303; inline void remin(int& a, int b) { a = min(a, b); } inline void remax(int& a, int b) { a = max(a, b); } struct MySet { int cnt[maxN], st, ad, mx; long long T[5]; void init() { cnt[0] = 1; T[0] = 1LL; st = 6, ad = 63, mx = 0; } void add(int v) { cnt[v]++; if (cnt[v] == 1) { int i = v >> st, j = v & ad; T[i] |= 1LL << j; remax(mx, v); } } void ers(int v) { cnt[v]--; if (cnt[v] == 0) { int i = v >> st, j = v & ad; T[i] ^= 1LL << j; } } void updt() { if (cnt[mx] == 0) for (int i=(mx>>st); i>=0; i--) if (T[i] != 0) { mx = (i+1) * 64 - 1 - __builtin_clzll(T[i]); break; } } } Set; vector <int> G[maxN], H[maxN]; bool erased[maxN]; int n, in[maxN], pref[maxN], out[maxN], suf[maxN], dp[maxN], dmn[3][maxN], res = maxN; int myk() { FOR(v, 1, n+1) { in[v] = 0; for (int u : H[v]) if(!erased[u]) remax(in[v], in[u]); in[v]++; pref[v] = (pref[v - 1] < in[v] and !erased[v]) ? in[v] : pref[v - 1]; } for (int v=n; v>=1; v--) { out[v] = 0; for (int u : G[v]) if(!erased[u]) remax(out[v], out[u]); out[v]++; suf[v] = (suf[v + 1] < out[v] and !erased[v]) ? out[v] : suf[v + 1]; } int ret = n; FOR(v, 1, n+1) { if (erased[v]) continue; for (int u : H[v]) if(!erased[u]) Set.ers(in[u] + out[v]); Set.updt(); int hisret = max({pref[v - 1], suf[v + 1], Set.mx}); remin(ret, hisret); for (int u : G[v]) if(!erased[u]) Set.add(in[v] + out[u]); } return ret; } int filldmn(int* d) { int maxlen = 0; for (int v=n; v>=1; v--) { dp[v] = 0; if (erased[v]) continue; for (int u : G[v]) if (!erased[u]) remax(dp[v], dp[u]); remax(maxlen, ++dp[v]); } int v = 1, s = 0; while (dp[v] != maxlen) v++; while (dp[v] != 1) { d[s++] = v; for (int u : G[v]) if (!erased[u] and dp[u] == dp[v] - 1) { v = u; break; } } d[s++] = v; return s; } void go(int depth) { if (depth == -1) { remin(res, myk()); return; } int *d = dmn[depth], s = filldmn(d); FOR(i, 0, s) { erased[d[i]] = true; go(depth - 1); erased[d[i]] = false; } } void ext(int ans) { printf("%d\n", ans); exit(0); } int main() { int m, k; scanf ("%d%d%d", &n, &m, &k); while (m--) { int a, b; scanf ("%d%d", &a, &b); G[a].pb(b); H[b].pb(a); } Set.init(); if (k == n) ext(0); if (k == 0) ext(filldmn(dmn[0])); go(k-2); printf("%d\n", res); 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 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 156 157 158 159 160 161 162 163 | #include <bits/stdc++.h> #define FOR(i, a, b) for (int i=(a); (i)<(b); (i)++) #define PPC(x) __builtin_popcountll((x)) #define ALL(x) (x).begin(), (x).end() #define pb push_back using namespace std; const int maxN = 303; inline void remin(int& a, int b) { a = min(a, b); } inline void remax(int& a, int b) { a = max(a, b); } struct MySet { int cnt[maxN], st, ad, mx; long long T[5]; void init() { cnt[0] = 1; T[0] = 1LL; st = 6, ad = 63, mx = 0; } void add(int v) { cnt[v]++; if (cnt[v] == 1) { int i = v >> st, j = v & ad; T[i] |= 1LL << j; remax(mx, v); } } void ers(int v) { cnt[v]--; if (cnt[v] == 0) { int i = v >> st, j = v & ad; T[i] ^= 1LL << j; } } void updt() { if (cnt[mx] == 0) for (int i=(mx>>st); i>=0; i--) if (T[i] != 0) { mx = (i+1) * 64 - 1 - __builtin_clzll(T[i]); break; } } } Set; vector <int> G[maxN], H[maxN]; bool erased[maxN]; int n, in[maxN], pref[maxN], out[maxN], suf[maxN], dp[maxN], dmn[3][maxN], res = maxN; int myk() { FOR(v, 1, n+1) { in[v] = 0; for (int u : H[v]) if(!erased[u]) remax(in[v], in[u]); in[v]++; pref[v] = (pref[v - 1] < in[v] and !erased[v]) ? in[v] : pref[v - 1]; } for (int v=n; v>=1; v--) { out[v] = 0; for (int u : G[v]) if(!erased[u]) remax(out[v], out[u]); out[v]++; suf[v] = (suf[v + 1] < out[v] and !erased[v]) ? out[v] : suf[v + 1]; } int ret = n; FOR(v, 1, n+1) { if (erased[v]) continue; for (int u : H[v]) if(!erased[u]) Set.ers(in[u] + out[v]); Set.updt(); int hisret = max({pref[v - 1], suf[v + 1], Set.mx}); remin(ret, hisret); for (int u : G[v]) if(!erased[u]) Set.add(in[v] + out[u]); } return ret; } int filldmn(int* d) { int maxlen = 0; for (int v=n; v>=1; v--) { dp[v] = 0; if (erased[v]) continue; for (int u : G[v]) if (!erased[u]) remax(dp[v], dp[u]); remax(maxlen, ++dp[v]); } int v = 1, s = 0; while (dp[v] != maxlen) v++; while (dp[v] != 1) { d[s++] = v; for (int u : G[v]) if (!erased[u] and dp[u] == dp[v] - 1) { v = u; break; } } d[s++] = v; return s; } void go(int depth) { if (depth == -1) { remin(res, myk()); return; } int *d = dmn[depth], s = filldmn(d); FOR(i, 0, s) { erased[d[i]] = true; go(depth - 1); erased[d[i]] = false; } } void ext(int ans) { printf("%d\n", ans); exit(0); } int main() { int m, k; scanf ("%d%d%d", &n, &m, &k); while (m--) { int a, b; scanf ("%d%d", &a, &b); G[a].pb(b); H[b].pb(a); } Set.init(); if (k == n) ext(0); if (k == 0) ext(filldmn(dmn[0])); go(k-2); printf("%d\n", res); return 0; } |