#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 500000
#define M 500000
int *ej[N], eo[N], *fi[N], fo[N], rr[N], qu[N], qu_[N], ii_[N], cnt;
void append(int **ej, int *eo, int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
void dfs1(int i) {
int o;
if (rr[i])
return;
rr[i] = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
dfs1(j);
}
qu[--cnt] = i;
}
void dfs2(int j, int r) {
int o;
if (rr[j] != -1)
return;
qu_[cnt++] = j;
rr[j] = r;
for (o = fo[j]; o--; ) {
int i = fi[j][o];
dfs2(i, r);
}
}
int ds[N * 2];
int find(int i) {
return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}
void join(int i, int j) {
i = find(i);
j = find(j);
if (i == j)
return;
if (ds[i] > ds[j])
ds[i] = j;
else {
if (ds[i] == ds[j])
ds[i]--;
ds[j] = i;
}
}
int cc[N]; char done[N];
void mark(int j, int c) {
int o;
if (done[j])
return;
cc[j] = c, done[j] = 1;
if (c == 0) {
for (o = fo[j]; o--; ) {
int i = fi[j][o];
if (!done[i] && --eo[i] == 0)
ii_[cnt++] = i;
}
} else {
for (o = fo[j]; o--; ) {
int i = fi[j][o];
mark(i, 0);
}
}
}
int main() {
static int ii[M], jj[M];
int n, m, h, h_, h1, i, j;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
fi[i] = (int *) malloc(2 * sizeof *fi[i]);
}
for (h = 0; h < m; h++) {
scanf("%d%d", &i, &j), i--, j--;
ii[h] = i, jj[h] = j;
append(ej, eo, i, j), append(fi, fo, j, i);
}
cnt = n;
for (i = 0; i < n; i++)
dfs1(i);
cnt = 0;
for (h = 0; h < n; h++) {
i = qu[h];
dfs2(i, i);
}
memset(ds, -1, n * 2 * sizeof *ds);
for (h = 0; h < m; h++) {
i = ii[h], j = jj[h];
if (rr[i] == rr[j])
join(i << 1 | 0, j << 1 | 1), join(j << 1 | 0, i << 1 | 1);
}
for (i = 0; i < n; i++)
cc[i] = find(i << 1 | 0) < find(i << 1 | 1) ? 0 : 1;
cnt = 0;
for (i = 0; i < n; i++)
if (eo[i] == 0)
ii_[cnt++] = i;
for (h = n - 1; h >= 0; h = h_) {
while (cnt)
mark(ii_[--cnt], 1);
h_ = h - 1;
while (h_ >= 0 && rr[qu_[h_]] == rr[qu_[h]])
h_--;
for (h1 = h; h1 > h_; h1--)
mark(qu_[h1], cc[qu_[h1]]);
}
cnt = 0;
for (i = 0; i < n; i++)
if (cc[i])
ii_[cnt++] = i;
printf("%d\n", cnt);
while (cnt--)
printf("%d ", ii_[cnt] + 1);
printf("\n");
return 0;
}
Compilation message
Main.c: In function 'append':
Main.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
13 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'main':
Main.c:94:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:100:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
188 ms |
90620 KB |
Output is correct |
2 |
Correct |
128 ms |
63480 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
67 ms |
54424 KB |
Output is correct |
5 |
Correct |
97 ms |
61388 KB |
Output is correct |
6 |
Correct |
142 ms |
61324 KB |
Output is correct |
7 |
Correct |
109 ms |
60320 KB |
Output is correct |
8 |
Correct |
141 ms |
62136 KB |
Output is correct |
9 |
Correct |
296 ms |
63568 KB |
Output is correct |
10 |
Correct |
292 ms |
58544 KB |
Output is correct |
11 |
Correct |
303 ms |
62728 KB |
Output is correct |
12 |
Correct |
338 ms |
59164 KB |
Output is correct |
13 |
Correct |
240 ms |
60780 KB |
Output is correct |
14 |
Correct |
212 ms |
61156 KB |
Output is correct |
15 |
Correct |
195 ms |
61164 KB |
Output is correct |
16 |
Correct |
178 ms |
61040 KB |
Output is correct |
17 |
Correct |
40 ms |
7068 KB |
Output is correct |
18 |
Correct |
65 ms |
10940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
148 ms |
90484 KB |
Output is correct |
2 |
Correct |
133 ms |
62504 KB |
Output is correct |
3 |
Correct |
100 ms |
57532 KB |
Output is correct |
4 |
Correct |
284 ms |
61236 KB |
Output is correct |
5 |
Correct |
386 ms |
63676 KB |
Output is correct |
6 |
Correct |
236 ms |
60576 KB |
Output is correct |
7 |
Correct |
213 ms |
65820 KB |
Output is correct |
8 |
Correct |
217 ms |
65716 KB |
Output is correct |
9 |
Correct |
179 ms |
65136 KB |
Output is correct |
10 |
Correct |
52 ms |
7488 KB |
Output is correct |
11 |
Correct |
28 ms |
8704 KB |
Output is correct |
12 |
Correct |
31 ms |
8804 KB |
Output is correct |
13 |
Correct |
167 ms |
91656 KB |
Output is correct |
14 |
Correct |
134 ms |
69848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1236 KB |
Output is correct |
2 |
Correct |
2 ms |
980 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
980 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
2 ms |
832 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
1196 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
1004 KB |
Output is correct |
16 |
Correct |
2 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
436 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
188 ms |
90620 KB |
Output is correct |
2 |
Correct |
128 ms |
63480 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
67 ms |
54424 KB |
Output is correct |
5 |
Correct |
97 ms |
61388 KB |
Output is correct |
6 |
Correct |
142 ms |
61324 KB |
Output is correct |
7 |
Correct |
109 ms |
60320 KB |
Output is correct |
8 |
Correct |
141 ms |
62136 KB |
Output is correct |
9 |
Correct |
296 ms |
63568 KB |
Output is correct |
10 |
Correct |
292 ms |
58544 KB |
Output is correct |
11 |
Correct |
303 ms |
62728 KB |
Output is correct |
12 |
Correct |
338 ms |
59164 KB |
Output is correct |
13 |
Correct |
240 ms |
60780 KB |
Output is correct |
14 |
Correct |
212 ms |
61156 KB |
Output is correct |
15 |
Correct |
195 ms |
61164 KB |
Output is correct |
16 |
Correct |
178 ms |
61040 KB |
Output is correct |
17 |
Correct |
40 ms |
7068 KB |
Output is correct |
18 |
Correct |
65 ms |
10940 KB |
Output is correct |
19 |
Correct |
148 ms |
90484 KB |
Output is correct |
20 |
Correct |
133 ms |
62504 KB |
Output is correct |
21 |
Correct |
100 ms |
57532 KB |
Output is correct |
22 |
Correct |
284 ms |
61236 KB |
Output is correct |
23 |
Correct |
386 ms |
63676 KB |
Output is correct |
24 |
Correct |
236 ms |
60576 KB |
Output is correct |
25 |
Correct |
213 ms |
65820 KB |
Output is correct |
26 |
Correct |
217 ms |
65716 KB |
Output is correct |
27 |
Correct |
179 ms |
65136 KB |
Output is correct |
28 |
Correct |
52 ms |
7488 KB |
Output is correct |
29 |
Correct |
28 ms |
8704 KB |
Output is correct |
30 |
Correct |
31 ms |
8804 KB |
Output is correct |
31 |
Correct |
167 ms |
91656 KB |
Output is correct |
32 |
Correct |
134 ms |
69848 KB |
Output is correct |
33 |
Correct |
2 ms |
1236 KB |
Output is correct |
34 |
Correct |
2 ms |
980 KB |
Output is correct |
35 |
Correct |
2 ms |
852 KB |
Output is correct |
36 |
Correct |
2 ms |
980 KB |
Output is correct |
37 |
Correct |
2 ms |
980 KB |
Output is correct |
38 |
Correct |
2 ms |
980 KB |
Output is correct |
39 |
Correct |
2 ms |
980 KB |
Output is correct |
40 |
Correct |
2 ms |
832 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
2 ms |
1196 KB |
Output is correct |
45 |
Correct |
2 ms |
980 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
2 ms |
1004 KB |
Output is correct |
48 |
Correct |
2 ms |
980 KB |
Output is correct |
49 |
Correct |
2 ms |
980 KB |
Output is correct |
50 |
Correct |
1 ms |
436 KB |
Output is correct |
51 |
Correct |
1 ms |
596 KB |
Output is correct |
52 |
Correct |
152 ms |
95692 KB |
Output is correct |
53 |
Correct |
139 ms |
65152 KB |
Output is correct |
54 |
Correct |
118 ms |
60180 KB |
Output is correct |
55 |
Correct |
267 ms |
66116 KB |
Output is correct |
56 |
Correct |
402 ms |
69056 KB |
Output is correct |
57 |
Correct |
229 ms |
65596 KB |
Output is correct |
58 |
Correct |
213 ms |
65868 KB |
Output is correct |
59 |
Correct |
198 ms |
65852 KB |
Output is correct |
60 |
Correct |
40 ms |
8104 KB |
Output is correct |
61 |
Correct |
29 ms |
8584 KB |
Output is correct |
62 |
Correct |
30 ms |
8876 KB |
Output is correct |
63 |
Correct |
170 ms |
91816 KB |
Output is correct |
64 |
Correct |
154 ms |
67892 KB |
Output is correct |
65 |
Correct |
34 ms |
9392 KB |
Output is correct |
66 |
Correct |
349 ms |
66132 KB |
Output is correct |
67 |
Correct |
231 ms |
66824 KB |
Output is correct |
68 |
Correct |
224 ms |
66084 KB |
Output is correct |
69 |
Correct |
42 ms |
7752 KB |
Output is correct |
70 |
Correct |
102 ms |
25896 KB |
Output is correct |