#include <bits/stdc++.h> using namespace std; #define e1 first #define e2 second #define pb push_back #define mp make_pair #define boost ios_base::sync_with_stdio(false) #define eb emplace_back #define OUT(x) {cout << x; exit(0); } #define FOR(i, a, b) for (int i=(a); i<=(b); ++i) typedef long long ll; typedef unsigned long long ull; typedef pair <int, int> PII; typedef pair <ll, ll> PLL; typedef pair <PLL, PLL> PP; typedef unsigned int ui; const int mod = 1e9+7; const int inf = 1e9+9; const ll MOD = 1e9+696969; const ll INF = 1e18; const int maxn = 305; vector <int> rev[maxn], v[maxn]; bool mark[maxn]; int dpP[maxn], accP[maxn], dpS[maxn], accS[maxn], ost[maxn], n, m, k; const int R = 512; int dr[2 * R + 5], result; inline void query(int x, int y, int val) { x += R-1; y += R-1; dr[x] = max(dr[x], val); dr[y] = max(dr[y], val); while (x + 1 < y) { if (!(x & 1)) dr[x + 1] = (dr[x + 1] >= val)?dr[x + 1] : val; if (y & 1) dr[y - 1] = (dr[y - 1] >= val)?dr[y - 1] : val; x >>= 1; y >>= 1; } } int CHE; int solve() { //mark has to be properly set fill(dr, dr+R+R+2, 0); dpP[0] = accP[0] = 0; FOR(i, 1, n) { if (mark[i]) { accP[i] = accP[i-1]; continue; } int step = 0; for (auto u : rev[i]) step = (!mark[u]&&dpP[u]>step)?dpP[u]: step; ++step; dpP[i] = step; accP[i] = (accP[i-1] >= step)?accP[i-1]:step; } dpS[n + 1] = accS[n + 1] = 0; for (int i=n; i>0; --i) { if (mark[i]) { accS[i] = accS[i+1]; continue; } int step = 0; for (auto u : v[i]) step = (!mark[u]&&dpS[u]>step)?dpS[u]:step; ++step; dpS[i] = step; accS[i] = (accS[i+1] >= step)?accS[i+1]:step; } FOR(i, 1, n) { if (mark[i]) continue; for (auto u: v[i]) { if (!mark[u] && i+2 <= u) query(i+1, u-1, dpP[i]+dpS[u]); } } for (int i=1; i<R; ++i) { dr[i << 1] = (dr[i << 1] >= dr[i])?dr[i << 1] : dr[i]; dr[i << 1 | 1] = (dr[i << 1 | 1] >= dr[i])?dr[i << 1 | 1] : dr[i]; } int ret = n + 1; FOR(i, 1, n) { if (mark[i]) continue; ret = min(ret, max(accP[i - 1], max(accS[i + 1], dr[i + R - 1]))); } return ret; } inline int solver(int m1, int m2, int m3) { mark[m1] = mark[m2] = mark[m3] = true; int w = solve(); mark[m1] = mark[m2] = mark[m3] = false; return w; } vector <int> longestPath() { vector <int> ret; int bet = 0, opt = 0; FOR(i, 1, n) { if (mark[i]) continue; int step = 0, os = 0; for (auto u : rev[i]) if (!mark[u] && dpP[u] > step) { step = dpP[u]; os = u; } ++step; dpP[i] = step; ost[i] = os; if (step > opt) { bet = i; opt = step; } } while (bet) { ret.pb(bet); bet = ost[bet]; } return ret; } void backtrack(int level) { if (level == 1) { result = min(result, solve()); return; } vector <int> longboi = longestPath(); int sz = longboi.size(); for (int i=0; i<sz; ++i) { int u = longboi[i]; mark[u] = true; backtrack(level - 1); mark[u] = false; } } int main() { boost; cin >> n >> m >> k; if (k == n) OUT(0); result = n + 1; FOR(i, 1, m) { int a, b; cin >> a >> b; v[a].pb(b); rev[b].pb(a); } if (k == 0) { solver(0, 0, 0); OUT(accP[n]); } backtrack(k); cout << result; }
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 164 165 | #include <bits/stdc++.h> using namespace std; #define e1 first #define e2 second #define pb push_back #define mp make_pair #define boost ios_base::sync_with_stdio(false) #define eb emplace_back #define OUT(x) {cout << x; exit(0); } #define FOR(i, a, b) for (int i=(a); i<=(b); ++i) typedef long long ll; typedef unsigned long long ull; typedef pair <int, int> PII; typedef pair <ll, ll> PLL; typedef pair <PLL, PLL> PP; typedef unsigned int ui; const int mod = 1e9+7; const int inf = 1e9+9; const ll MOD = 1e9+696969; const ll INF = 1e18; const int maxn = 305; vector <int> rev[maxn], v[maxn]; bool mark[maxn]; int dpP[maxn], accP[maxn], dpS[maxn], accS[maxn], ost[maxn], n, m, k; const int R = 512; int dr[2 * R + 5], result; inline void query(int x, int y, int val) { x += R-1; y += R-1; dr[x] = max(dr[x], val); dr[y] = max(dr[y], val); while (x + 1 < y) { if (!(x & 1)) dr[x + 1] = (dr[x + 1] >= val)?dr[x + 1] : val; if (y & 1) dr[y - 1] = (dr[y - 1] >= val)?dr[y - 1] : val; x >>= 1; y >>= 1; } } int CHE; int solve() { //mark has to be properly set fill(dr, dr+R+R+2, 0); dpP[0] = accP[0] = 0; FOR(i, 1, n) { if (mark[i]) { accP[i] = accP[i-1]; continue; } int step = 0; for (auto u : rev[i]) step = (!mark[u]&&dpP[u]>step)?dpP[u]: step; ++step; dpP[i] = step; accP[i] = (accP[i-1] >= step)?accP[i-1]:step; } dpS[n + 1] = accS[n + 1] = 0; for (int i=n; i>0; --i) { if (mark[i]) { accS[i] = accS[i+1]; continue; } int step = 0; for (auto u : v[i]) step = (!mark[u]&&dpS[u]>step)?dpS[u]:step; ++step; dpS[i] = step; accS[i] = (accS[i+1] >= step)?accS[i+1]:step; } FOR(i, 1, n) { if (mark[i]) continue; for (auto u: v[i]) { if (!mark[u] && i+2 <= u) query(i+1, u-1, dpP[i]+dpS[u]); } } for (int i=1; i<R; ++i) { dr[i << 1] = (dr[i << 1] >= dr[i])?dr[i << 1] : dr[i]; dr[i << 1 | 1] = (dr[i << 1 | 1] >= dr[i])?dr[i << 1 | 1] : dr[i]; } int ret = n + 1; FOR(i, 1, n) { if (mark[i]) continue; ret = min(ret, max(accP[i - 1], max(accS[i + 1], dr[i + R - 1]))); } return ret; } inline int solver(int m1, int m2, int m3) { mark[m1] = mark[m2] = mark[m3] = true; int w = solve(); mark[m1] = mark[m2] = mark[m3] = false; return w; } vector <int> longestPath() { vector <int> ret; int bet = 0, opt = 0; FOR(i, 1, n) { if (mark[i]) continue; int step = 0, os = 0; for (auto u : rev[i]) if (!mark[u] && dpP[u] > step) { step = dpP[u]; os = u; } ++step; dpP[i] = step; ost[i] = os; if (step > opt) { bet = i; opt = step; } } while (bet) { ret.pb(bet); bet = ost[bet]; } return ret; } void backtrack(int level) { if (level == 1) { result = min(result, solve()); return; } vector <int> longboi = longestPath(); int sz = longboi.size(); for (int i=0; i<sz; ++i) { int u = longboi[i]; mark[u] = true; backtrack(level - 1); mark[u] = false; } } int main() { boost; cin >> n >> m >> k; if (k == n) OUT(0); result = n + 1; FOR(i, 1, m) { int a, b; cin >> a >> b; v[a].pb(b); rev[b].pb(a); } if (k == 0) { solver(0, 0, 0); OUT(accP[n]); } backtrack(k); cout << result; } |