#include<bits/stdc++.h>
using namespace std;
mt19937 rnd(51);
#define ll long long
#define pb push_back
#define ld long double
const int N = 3e5 + 10, INF = 1e9, K = 600;
int n, k, in_block[N], out_block[N], in[N], out[N], block[N], cnt_out[N], cnt_in[N], q[N];
bool used[N];
vector<int> g[N], gr[N];
bool cycle = 0;
set<int> need;
void dfs_block(int u) {
int l = 0, r = 0;
q[r++] = u;
while (l < r) {
int v = q[l++];
if (v >= k && out_block[v] < in_block[v]) {
cycle = 1;
}
cnt_in[v]++;
assert(cnt_in[v] <= N / K + 1);
if (v >= k && !used[v] && out_block[v] == in_block[v]) {
need.insert(in_block[v]);
block[v] = in_block[v];
used[v] = 1;
}
for (auto to : g[v]) {
if (in_block[v] > in_block[to]) {
in_block[to] = in_block[v];
q[r++] = to;
}
}
}
}
void zhfs_block(int u) {
int l = 0, r = 0;
q[r++] = u;
while (l < r) {
int v = q[l++];
if (v >= k && out_block[v] < in_block[v]) {
cycle = 1;
}
cnt_out[v]++;
assert(cnt_out[v] <= N / K + 1);
if (v >= k && !used[v] && out_block[v] == in_block[v]) {
need.insert(in_block[v]);
block[v] = in_block[v];
used[v] = 1;
}
for (auto to : gr[v]) {
if (out_block[v] < out_block[to]) {
out_block[to] = out_block[v];
q[r++] = to;
}
}
}
}
void dfs(int u) {
int l = 0, r = 0;
q[r++] = u;
while (l < r) {
int v = q[l++];
if (v >= k && out[v] <= in[v]) {
cycle = 1;
}
for (auto to : g[v]) {
if (block[v] == block[to] && in[v] > in[to]) {
in[to] = in[v];
q[r++] = to;
}
}
}
}
void zhfs(int u) {
int l = 0, r = 0;
q[r++] = u;
while (l < r) {
int v = q[l++];
if (v >= k && out[v] <= in[v]) {
cycle = 1;
}
for (auto to : gr[v]) {
if (block[v] == block[to] && out[v] < out[to]) {
out[to] = out[v];
q[r++] = to;
}
}
}
}
int add_teleporter(int u, int v) {
if (max(u, v) < k) {
if (u >= v) {
cycle = 1;
}
} else {
g[u].pb(v);
gr[v].pb(u);
if (in_block[v] < in_block[u]) {
in_block[v] = in_block[u];
dfs_block(v);
}
if (out_block[u] > out_block[v]) {
out_block[u] = out_block[v];
zhfs_block(u);
}
for (auto v : need) {
for (int vert = v * K; vert < min(k, (v + 1) * K); vert++) {
zhfs(vert);
dfs(vert);
}
}
need.clear();
if (block[v] == block[u] && in[v] < in[u]) {
in[v] = in[u];
dfs(v);
}
if (block[v] == block[u] && out[u] > out[v]) {
out[u] = out[v];
zhfs(u);
}
}
return cycle;
}
void init(int n_, int k_) {
n = n_, k = k_;
for (int i = 0; i < n; i++) {
in_block[i] = -INF, out_block[i] = INF;
in[i] = -INF, out[i] = INF;
block[i] = -(i + 1);
}
for (int i = 0; i < k; i++) {
in_block[i] = out_block[i] = block[i] = i / K;
in[i] = out[i] = i;
}
}
#ifdef LOCAL
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<pair<int,int>> e(m);
for (int i = 0; i < m; i++) {
cin >> e[i].first >> e[i].second;
}
init(n, k);
int i;
for (i = 0; i < m; i++) {
int res = add_teleporter(e[i].first, e[i].second);
assert(res == 0 || res == 1);
if (res == 1) {
break;
}
}
cout << i << endl;
return 0;
}
#endif // LOCAL
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14324 KB |
Output is correct |
2 |
Correct |
8 ms |
14316 KB |
Output is correct |
3 |
Correct |
8 ms |
14416 KB |
Output is correct |
4 |
Correct |
8 ms |
14416 KB |
Output is correct |
5 |
Correct |
10 ms |
14328 KB |
Output is correct |
6 |
Correct |
8 ms |
14416 KB |
Output is correct |
7 |
Correct |
10 ms |
14416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14324 KB |
Output is correct |
2 |
Correct |
8 ms |
14316 KB |
Output is correct |
3 |
Correct |
8 ms |
14416 KB |
Output is correct |
4 |
Correct |
8 ms |
14416 KB |
Output is correct |
5 |
Correct |
10 ms |
14328 KB |
Output is correct |
6 |
Correct |
8 ms |
14416 KB |
Output is correct |
7 |
Correct |
10 ms |
14416 KB |
Output is correct |
8 |
Correct |
8 ms |
14388 KB |
Output is correct |
9 |
Correct |
10 ms |
14392 KB |
Output is correct |
10 |
Correct |
9 ms |
14408 KB |
Output is correct |
11 |
Correct |
8 ms |
14416 KB |
Output is correct |
12 |
Correct |
11 ms |
14416 KB |
Output is correct |
13 |
Correct |
9 ms |
14432 KB |
Output is correct |
14 |
Correct |
9 ms |
14416 KB |
Output is correct |
15 |
Correct |
8 ms |
14416 KB |
Output is correct |
16 |
Correct |
8 ms |
14424 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
13 ms |
14448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14324 KB |
Output is correct |
2 |
Correct |
8 ms |
14316 KB |
Output is correct |
3 |
Correct |
8 ms |
14416 KB |
Output is correct |
4 |
Correct |
8 ms |
14416 KB |
Output is correct |
5 |
Correct |
10 ms |
14328 KB |
Output is correct |
6 |
Correct |
8 ms |
14416 KB |
Output is correct |
7 |
Correct |
10 ms |
14416 KB |
Output is correct |
8 |
Correct |
8 ms |
14388 KB |
Output is correct |
9 |
Correct |
10 ms |
14392 KB |
Output is correct |
10 |
Correct |
9 ms |
14408 KB |
Output is correct |
11 |
Correct |
8 ms |
14416 KB |
Output is correct |
12 |
Correct |
11 ms |
14416 KB |
Output is correct |
13 |
Correct |
9 ms |
14432 KB |
Output is correct |
14 |
Correct |
9 ms |
14416 KB |
Output is correct |
15 |
Correct |
8 ms |
14416 KB |
Output is correct |
16 |
Correct |
8 ms |
14424 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
13 ms |
14448 KB |
Output is correct |
20 |
Correct |
9 ms |
14488 KB |
Output is correct |
21 |
Correct |
8 ms |
14404 KB |
Output is correct |
22 |
Correct |
10 ms |
14468 KB |
Output is correct |
23 |
Correct |
9 ms |
14528 KB |
Output is correct |
24 |
Correct |
13 ms |
14480 KB |
Output is correct |
25 |
Correct |
11 ms |
14544 KB |
Output is correct |
26 |
Correct |
13 ms |
14544 KB |
Output is correct |
27 |
Correct |
18 ms |
14512 KB |
Output is correct |
28 |
Correct |
13 ms |
14524 KB |
Output is correct |
29 |
Correct |
17 ms |
14488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14324 KB |
Output is correct |
2 |
Correct |
8 ms |
14316 KB |
Output is correct |
3 |
Correct |
8 ms |
14416 KB |
Output is correct |
4 |
Correct |
8 ms |
14416 KB |
Output is correct |
5 |
Correct |
10 ms |
14328 KB |
Output is correct |
6 |
Correct |
8 ms |
14416 KB |
Output is correct |
7 |
Correct |
10 ms |
14416 KB |
Output is correct |
8 |
Correct |
8 ms |
14388 KB |
Output is correct |
9 |
Correct |
10 ms |
14392 KB |
Output is correct |
10 |
Correct |
9 ms |
14408 KB |
Output is correct |
11 |
Correct |
8 ms |
14416 KB |
Output is correct |
12 |
Correct |
11 ms |
14416 KB |
Output is correct |
13 |
Correct |
9 ms |
14432 KB |
Output is correct |
14 |
Correct |
9 ms |
14416 KB |
Output is correct |
15 |
Correct |
8 ms |
14416 KB |
Output is correct |
16 |
Correct |
8 ms |
14424 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
13 ms |
14448 KB |
Output is correct |
20 |
Correct |
9 ms |
14488 KB |
Output is correct |
21 |
Correct |
8 ms |
14404 KB |
Output is correct |
22 |
Correct |
10 ms |
14468 KB |
Output is correct |
23 |
Correct |
9 ms |
14528 KB |
Output is correct |
24 |
Correct |
13 ms |
14480 KB |
Output is correct |
25 |
Correct |
11 ms |
14544 KB |
Output is correct |
26 |
Correct |
13 ms |
14544 KB |
Output is correct |
27 |
Correct |
18 ms |
14512 KB |
Output is correct |
28 |
Correct |
13 ms |
14524 KB |
Output is correct |
29 |
Correct |
17 ms |
14488 KB |
Output is correct |
30 |
Correct |
32 ms |
16404 KB |
Output is correct |
31 |
Correct |
15 ms |
15648 KB |
Output is correct |
32 |
Correct |
41 ms |
17128 KB |
Output is correct |
33 |
Correct |
32 ms |
17080 KB |
Output is correct |
34 |
Correct |
768 ms |
17228 KB |
Output is correct |
35 |
Correct |
257 ms |
17232 KB |
Output is correct |
36 |
Correct |
63 ms |
17180 KB |
Output is correct |
37 |
Correct |
104 ms |
17168 KB |
Output is correct |
38 |
Correct |
210 ms |
16888 KB |
Output is correct |
39 |
Correct |
254 ms |
16908 KB |
Output is correct |
40 |
Correct |
598 ms |
17416 KB |
Output is correct |
41 |
Correct |
163 ms |
16940 KB |
Output is correct |
42 |
Correct |
131 ms |
16924 KB |
Output is correct |
43 |
Correct |
63 ms |
17356 KB |
Output is correct |
44 |
Correct |
252 ms |
17244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14324 KB |
Output is correct |
2 |
Correct |
8 ms |
14316 KB |
Output is correct |
3 |
Correct |
8 ms |
14416 KB |
Output is correct |
4 |
Correct |
8 ms |
14416 KB |
Output is correct |
5 |
Correct |
10 ms |
14328 KB |
Output is correct |
6 |
Correct |
8 ms |
14416 KB |
Output is correct |
7 |
Correct |
10 ms |
14416 KB |
Output is correct |
8 |
Correct |
8 ms |
14388 KB |
Output is correct |
9 |
Correct |
10 ms |
14392 KB |
Output is correct |
10 |
Correct |
9 ms |
14408 KB |
Output is correct |
11 |
Correct |
8 ms |
14416 KB |
Output is correct |
12 |
Correct |
11 ms |
14416 KB |
Output is correct |
13 |
Correct |
9 ms |
14432 KB |
Output is correct |
14 |
Correct |
9 ms |
14416 KB |
Output is correct |
15 |
Correct |
8 ms |
14416 KB |
Output is correct |
16 |
Correct |
8 ms |
14424 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
13 ms |
14448 KB |
Output is correct |
20 |
Correct |
9 ms |
14488 KB |
Output is correct |
21 |
Correct |
8 ms |
14404 KB |
Output is correct |
22 |
Correct |
10 ms |
14468 KB |
Output is correct |
23 |
Correct |
9 ms |
14528 KB |
Output is correct |
24 |
Correct |
13 ms |
14480 KB |
Output is correct |
25 |
Correct |
11 ms |
14544 KB |
Output is correct |
26 |
Correct |
13 ms |
14544 KB |
Output is correct |
27 |
Correct |
18 ms |
14512 KB |
Output is correct |
28 |
Correct |
13 ms |
14524 KB |
Output is correct |
29 |
Correct |
17 ms |
14488 KB |
Output is correct |
30 |
Correct |
32 ms |
16404 KB |
Output is correct |
31 |
Correct |
15 ms |
15648 KB |
Output is correct |
32 |
Correct |
41 ms |
17128 KB |
Output is correct |
33 |
Correct |
32 ms |
17080 KB |
Output is correct |
34 |
Correct |
768 ms |
17228 KB |
Output is correct |
35 |
Correct |
257 ms |
17232 KB |
Output is correct |
36 |
Correct |
63 ms |
17180 KB |
Output is correct |
37 |
Correct |
104 ms |
17168 KB |
Output is correct |
38 |
Correct |
210 ms |
16888 KB |
Output is correct |
39 |
Correct |
254 ms |
16908 KB |
Output is correct |
40 |
Correct |
598 ms |
17416 KB |
Output is correct |
41 |
Correct |
163 ms |
16940 KB |
Output is correct |
42 |
Correct |
131 ms |
16924 KB |
Output is correct |
43 |
Correct |
63 ms |
17356 KB |
Output is correct |
44 |
Correct |
252 ms |
17244 KB |
Output is correct |
45 |
Correct |
327 ms |
34036 KB |
Output is correct |
46 |
Correct |
18 ms |
20888 KB |
Output is correct |
47 |
Correct |
19 ms |
20860 KB |
Output is correct |
48 |
Correct |
488 ms |
42572 KB |
Output is correct |
49 |
Correct |
408 ms |
41608 KB |
Output is correct |
50 |
Execution timed out |
4043 ms |
34372 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |