#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
typedef pair < int, int > PII;
typedef long long LL;
const int N = 300 + 7, LIMIT = 72e4;
const LL M = 1e9 + 7LL;
vector < int > G[N];
bool Blocked[N];
int In[N], D[N], T[N];
queue < int > Q;
LL H[N];
unordered_set < LL > S;
int n, k, result, calls;
int Bfs ()
{
for (int i = 1; i <= n; ++i)
T[i] = In[i],
D[i] = 1;
for (int i = 1; i <= n; ++i)
if (Blocked[i])
for (int s : G[i])
--T[s];
for (int i = 1; i <= n; ++i)
if (!T[i] && !Blocked[i])
Q.push(i);
int maxi = 0;
while (!Q.empty())
{
int v = Q.front();
Q.pop();
int d = D[v];
maxi = max(maxi, d);
for (int s : G[v])
{
if (Blocked[s])
continue;
D[s] = max(D[s], d + 1);
if (--T[s] == 0)
Q.push(s);
}
}
return maxi;
}
bool Go (int r, LL h)
{
if (S.find(h) != S.end())
return true;
S.insert(h);
++calls;
if (r == 0)
{
result = min(result, Bfs());
return true;
}
if (calls > LIMIT)
return false;
int maxi = Bfs();
vector < PII > V;
for (int i = 1; i <= n; ++i)
if (!Blocked[i])
V.push_back({abs(2 * D[i] - result - 1), i});
sort(V.begin(), V.end());
for (int i = 0; i < V.size(); ++i)
{
Blocked[V[i].nd] = true;
if (!Go(r - 1, (h * H[V[i].nd]) % M))
return false;
Blocked[V[i].nd] = false;
}
return true;
}
void Read ()
{
int m;
scanf("%d %d %d", &n, &m, &k);
for (int i = 0; i < m; ++i)
{
int u, v;
scanf("%d %d", &u, &v);
++In[v];
G[u].push_back(v);
}
}
void Solve ()
{
for (int i = 1; i <= n; ++i)
H[i] = rand() % (M - 2LL) + 2LL;
result = n;
Go(k, 1LL);
printf("%d", result);
}
int main ()
{
srand(time(NULL));
Read();
Solve();
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 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second typedef pair < int, int > PII; typedef long long LL; const int N = 300 + 7, LIMIT = 72e4; const LL M = 1e9 + 7LL; vector < int > G[N]; bool Blocked[N]; int In[N], D[N], T[N]; queue < int > Q; LL H[N]; unordered_set < LL > S; int n, k, result, calls; int Bfs () { for (int i = 1; i <= n; ++i) T[i] = In[i], D[i] = 1; for (int i = 1; i <= n; ++i) if (Blocked[i]) for (int s : G[i]) --T[s]; for (int i = 1; i <= n; ++i) if (!T[i] && !Blocked[i]) Q.push(i); int maxi = 0; while (!Q.empty()) { int v = Q.front(); Q.pop(); int d = D[v]; maxi = max(maxi, d); for (int s : G[v]) { if (Blocked[s]) continue; D[s] = max(D[s], d + 1); if (--T[s] == 0) Q.push(s); } } return maxi; } bool Go (int r, LL h) { if (S.find(h) != S.end()) return true; S.insert(h); ++calls; if (r == 0) { result = min(result, Bfs()); return true; } if (calls > LIMIT) return false; int maxi = Bfs(); vector < PII > V; for (int i = 1; i <= n; ++i) if (!Blocked[i]) V.push_back({abs(2 * D[i] - result - 1), i}); sort(V.begin(), V.end()); for (int i = 0; i < V.size(); ++i) { Blocked[V[i].nd] = true; if (!Go(r - 1, (h * H[V[i].nd]) % M)) return false; Blocked[V[i].nd] = false; } return true; } void Read () { int m; scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < m; ++i) { int u, v; scanf("%d %d", &u, &v); ++In[v]; G[u].push_back(v); } } void Solve () { for (int i = 1; i <= n; ++i) H[i] = rand() % (M - 2LL) + 2LL; result = n; Go(k, 1LL); printf("%d", result); } int main () { srand(time(NULL)); Read(); Solve(); return 0; } |
English