#include<bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = a; i <= b; i++) #define REP(i, a) FOR(i, 0, a - 1) #define ST first #define ND second #define V vector #define RS resize #define EB emplace_back #define ALL(a) a.begin(), a.end() #define S(a) (int)a.size() template<class T> void db(T a) { cerr << a; } template<class L, class R> void db(pair<L, L> a) { cerr << "(" << a.ST << ", " << a.ND << ")"; } template<class T> void db(V<T> v) { cerr << "{"; REP(i, S(v)) cerr << (i != 0 ? ", " : ""), db(v[i]); cerr << "}"; } template<class T> void dump(const char *s, T a) { cerr << s << ": "; db(a); cerr << "\n"; } template<class T, class... TS> void dump(const char *s, T a, TS... x) { while(*s != ',') cerr<< *s++; cerr << ": "; db(a); dump(s + 1, x...); } #ifdef DEBUG #define DB(...) dump(#__VA_ARGS__, __VA_ARGS__); #else #define DB(...) #endif using LL = long long; using PII = pair<int, int>; using VI = V<int>; using VLL = V<LL>; using VVI = V<VI>; using VPII = V<PII>; VVI g; VI used; V<bool> removed; VI dp; int called = 0; int to_add; int calc() { int ans = 0; called++; for(int i = S(used) - 1; i >= 0; i--) { used[i] = -1; dp[i] = 0; if(removed[i]) continue; for(int x : g[i]) dp[i] = max(dp[i], dp[x]); for(int x : g[i]) { if(dp[i] == dp[x] && used[i] == -1) used[i] = x; } dp[i]++; ans = max(ans, dp[i]); } return ans; } int backtrack(int rec) { if(rec == 0) return calc(); int mx = calc(); int n = S(dp); V<bool> added(n); VI s; REP(i, n) if(dp[i] == mx) { int p = i; while(p != -1) { s.EB(p); p = used[p]; } break; } int best = 1e9; for(int x : s) { removed[x] = 1; best = min(best, backtrack(rec - 1)); removed[x] = 0; if(called * to_add > 200000000) break; } return best; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; g.RS(n); used.RS(n); dp.RS(n); removed.RS(n); REP(i, m) { int a, b; cin >> a >> b; g[a - 1].EB(b - 1); } to_add = n + m; cout << backtrack(k) << "\n"; 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 | #include<bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = a; i <= b; i++) #define REP(i, a) FOR(i, 0, a - 1) #define ST first #define ND second #define V vector #define RS resize #define EB emplace_back #define ALL(a) a.begin(), a.end() #define S(a) (int)a.size() template<class T> void db(T a) { cerr << a; } template<class L, class R> void db(pair<L, L> a) { cerr << "(" << a.ST << ", " << a.ND << ")"; } template<class T> void db(V<T> v) { cerr << "{"; REP(i, S(v)) cerr << (i != 0 ? ", " : ""), db(v[i]); cerr << "}"; } template<class T> void dump(const char *s, T a) { cerr << s << ": "; db(a); cerr << "\n"; } template<class T, class... TS> void dump(const char *s, T a, TS... x) { while(*s != ',') cerr<< *s++; cerr << ": "; db(a); dump(s + 1, x...); } #ifdef DEBUG #define DB(...) dump(#__VA_ARGS__, __VA_ARGS__); #else #define DB(...) #endif using LL = long long; using PII = pair<int, int>; using VI = V<int>; using VLL = V<LL>; using VVI = V<VI>; using VPII = V<PII>; VVI g; VI used; V<bool> removed; VI dp; int called = 0; int to_add; int calc() { int ans = 0; called++; for(int i = S(used) - 1; i >= 0; i--) { used[i] = -1; dp[i] = 0; if(removed[i]) continue; for(int x : g[i]) dp[i] = max(dp[i], dp[x]); for(int x : g[i]) { if(dp[i] == dp[x] && used[i] == -1) used[i] = x; } dp[i]++; ans = max(ans, dp[i]); } return ans; } int backtrack(int rec) { if(rec == 0) return calc(); int mx = calc(); int n = S(dp); V<bool> added(n); VI s; REP(i, n) if(dp[i] == mx) { int p = i; while(p != -1) { s.EB(p); p = used[p]; } break; } int best = 1e9; for(int x : s) { removed[x] = 1; best = min(best, backtrack(rec - 1)); removed[x] = 0; if(called * to_add > 200000000) break; } return best; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; g.RS(n); used.RS(n); dp.RS(n); removed.RS(n); REP(i, m) { int a, b; cin >> a >> b; g[a - 1].EB(b - 1); } to_add = n + m; cout << backtrack(k) << "\n"; return 0; } |