/*{{{*/ #include <bits/stdc++.h> #ifdef LOCAL int c_; #define cout (c_?cerr:cout) #define debug(...) {cerr<<(c_?"":"\033[96;1m");\ ++c_;__VA_ARGS__;--c_;cerr<<(c_?"":"\033[0m");} #else #define debug(...) {} #endif #define st first #define nd second #define all(...) begin(__VA_ARGS__), end(__VA_ARGS__) #define dump(...) debug(print(#__VA_ARGS__" =", __VA_ARGS__)) #define dump_range(...) debug(cerr<<#__VA_ARGS__": ";print_range(__VA_ARGS__);) using namespace std; template< typename t > using V = vector< t >; using ll = long long; using ld = long double; void print(){cout<<'\n';} template< typename t, typename... v > void print(const t& a, const v&... b) {cout<<a<<(sizeof...(b)?" ":"");print(b...);} template< typename t > void print_range(t a, const t& b) {while (a!=b) cout<<(*a++)<<' ';cout<<'\n';} /*}}}*/ using ull = unsigned long long; constexpr int nax = 300, op = 3e8; int n, m, k, res, st; ull dp[nax + 1]; ull dp2[nax + 1]; int rs[nax + 1]; V< int > gr[nax + 1]; V< int > rg[nax + 1]; pair< ull, int > ilo[nax + 1]; bool lk[nax + 1], bl[nax + 1]; void resuj() { for (int i = 1; i <= n; ++i) rs[i] = (not bl[i]); for (int i = n; i >= 1; --i) if (not bl[i]) for (int s : gr[i]) if (not bl[s]) rs[i] = max(rs[i], rs[s] + 1); int r = *max_element(rs + 1, rs + n + 1); res = min(r, res); } void heuruj() { for (int i = 1; i <= n; ++i) dp[i] = dp2[i] = not bl[i]; for (int i = n; i >= 1; --i) if (not bl[i]) for (int s : gr[i]) if (not bl[s]) dp[i] += dp[s] + 1; for (int i = 1; i <= n; ++i) if (not bl[i]) for (int s : rg[i]) if (not bl[s]) dp2[i] += dp2[s] + 1; for (int i = 1; i <= n; ++i) if (not lk[i]) ilo[i] = {dp[i] * dp2[i], i}; else ilo[i] = {0, i}; } void heura(int il) { if (il == k) { resuj(); //for (int i = 1; i <= n; ++i) //if (bl[i]) //cout << i << ' '; //cout << '\n'; return; } heuruj(); sort(ilo + 1, ilo + n + 1); V< int > kand; for (int i = n; i >= max(n - st + 1, 0); --i) if (not lk[ilo[i].nd]) kand.push_back(ilo[i].nd); for (int v : kand) { lk[v] = bl[v] = true; heura(il + 1); bl[v] = false; } for (int v : kand) lk[v] = false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> k; res = n; st = op / (n + m); if (k >= 3) st = sqrt(sqrt(st)); else if (k == 2) st = sqrt(st); cerr << st << '\n'; for (int i = 0; i < m; ++i) { int p, q; cin >> p >> q; gr[p].push_back(q); rg[q].push_back(p); } heura(0); print(res); }
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 | /*{{{*/ #include <bits/stdc++.h> #ifdef LOCAL int c_; #define cout (c_?cerr:cout) #define debug(...) {cerr<<(c_?"":"\033[96;1m");\ ++c_;__VA_ARGS__;--c_;cerr<<(c_?"":"\033[0m");} #else #define debug(...) {} #endif #define st first #define nd second #define all(...) begin(__VA_ARGS__), end(__VA_ARGS__) #define dump(...) debug(print(#__VA_ARGS__" =", __VA_ARGS__)) #define dump_range(...) debug(cerr<<#__VA_ARGS__": ";print_range(__VA_ARGS__);) using namespace std; template< typename t > using V = vector< t >; using ll = long long; using ld = long double; void print(){cout<<'\n';} template< typename t, typename... v > void print(const t& a, const v&... b) {cout<<a<<(sizeof...(b)?" ":"");print(b...);} template< typename t > void print_range(t a, const t& b) {while (a!=b) cout<<(*a++)<<' ';cout<<'\n';} /*}}}*/ using ull = unsigned long long; constexpr int nax = 300, op = 3e8; int n, m, k, res, st; ull dp[nax + 1]; ull dp2[nax + 1]; int rs[nax + 1]; V< int > gr[nax + 1]; V< int > rg[nax + 1]; pair< ull, int > ilo[nax + 1]; bool lk[nax + 1], bl[nax + 1]; void resuj() { for (int i = 1; i <= n; ++i) rs[i] = (not bl[i]); for (int i = n; i >= 1; --i) if (not bl[i]) for (int s : gr[i]) if (not bl[s]) rs[i] = max(rs[i], rs[s] + 1); int r = *max_element(rs + 1, rs + n + 1); res = min(r, res); } void heuruj() { for (int i = 1; i <= n; ++i) dp[i] = dp2[i] = not bl[i]; for (int i = n; i >= 1; --i) if (not bl[i]) for (int s : gr[i]) if (not bl[s]) dp[i] += dp[s] + 1; for (int i = 1; i <= n; ++i) if (not bl[i]) for (int s : rg[i]) if (not bl[s]) dp2[i] += dp2[s] + 1; for (int i = 1; i <= n; ++i) if (not lk[i]) ilo[i] = {dp[i] * dp2[i], i}; else ilo[i] = {0, i}; } void heura(int il) { if (il == k) { resuj(); //for (int i = 1; i <= n; ++i) //if (bl[i]) //cout << i << ' '; //cout << '\n'; return; } heuruj(); sort(ilo + 1, ilo + n + 1); V< int > kand; for (int i = n; i >= max(n - st + 1, 0); --i) if (not lk[ilo[i].nd]) kand.push_back(ilo[i].nd); for (int v : kand) { lk[v] = bl[v] = true; heura(il + 1); bl[v] = false; } for (int v : kand) lk[v] = false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> k; res = n; st = op / (n + m); if (k >= 3) st = sqrt(sqrt(st)); else if (k == 2) st = sqrt(st); cerr << st << '\n'; for (int i = 0; i < m; ++i) { int p, q; cin >> p >> q; gr[p].push_back(q); rg[q].push_back(p); } heura(0); print(res); } |