#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define forn(i, n) for(int i=0; i<(int)n; ++i)
#define PB push_back
const int MAXN = 500010;
int n, m, cn;
bool vis[MAXN], ans[MAXN], del[MAXN];
vector<int> g[MAXN], gi[MAXN], order, toDel;
void dfs1(int v){
vis[v]=true;
for(int to:gi[v]) if(!vis[to]) dfs1(to);
order.PB(v);
}
void dfs2(int v, bool flag){
del[v]=true;
if(flag){
ans[v]=true;
for(int to:gi[v]) toDel.PB(to);
}
for(int to:g[v]) if(!del[to]) dfs2(to, !flag);
}
int main(){
scanf("%d %d", &n, &m);
forn(i, m){
int a, b; scanf("%d %d", &a, &b); --a, --b;
g[a].PB(b), gi[b].PB(a);
}
forn(i, n) if(!vis[i]) dfs1(i);
reverse(order.begin(), order.end());
for(int el:order) if(!del[el]){
dfs2(el, true);
for(int de:toDel) del[de]=true;
toDel.clear();
}
forn(i, n) cn+=ans[i];
printf("%d\n", cn);
forn(i, n) if(ans[i]) printf("%d ", i+1);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:29:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:31:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | int a, b; scanf("%d %d", &a, &b); --a, --b;
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
60184 KB |
Output is correct |
2 |
Correct |
157 ms |
59524 KB |
Output is correct |
3 |
Correct |
12 ms |
23792 KB |
Output is correct |
4 |
Correct |
55 ms |
30360 KB |
Output is correct |
5 |
Correct |
100 ms |
45376 KB |
Output is correct |
6 |
Correct |
142 ms |
47232 KB |
Output is correct |
7 |
Correct |
120 ms |
47672 KB |
Output is correct |
8 |
Correct |
151 ms |
46884 KB |
Output is correct |
9 |
Correct |
317 ms |
53416 KB |
Output is correct |
10 |
Correct |
222 ms |
51216 KB |
Output is correct |
11 |
Correct |
289 ms |
50676 KB |
Output is correct |
12 |
Correct |
294 ms |
46352 KB |
Output is correct |
13 |
Correct |
212 ms |
46784 KB |
Output is correct |
14 |
Correct |
209 ms |
46912 KB |
Output is correct |
15 |
Correct |
207 ms |
47068 KB |
Output is correct |
16 |
Correct |
191 ms |
47136 KB |
Output is correct |
17 |
Correct |
58 ms |
27728 KB |
Output is correct |
18 |
Correct |
82 ms |
29968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
155 ms |
60108 KB |
Output is correct |
2 |
Correct |
146 ms |
47668 KB |
Output is correct |
3 |
Correct |
112 ms |
46552 KB |
Output is correct |
4 |
Correct |
285 ms |
50136 KB |
Output is correct |
5 |
Correct |
402 ms |
50008 KB |
Output is correct |
6 |
Correct |
245 ms |
53944 KB |
Output is correct |
7 |
Correct |
231 ms |
54260 KB |
Output is correct |
8 |
Correct |
210 ms |
54108 KB |
Output is correct |
9 |
Correct |
195 ms |
54100 KB |
Output is correct |
10 |
Correct |
59 ms |
28824 KB |
Output is correct |
11 |
Correct |
45 ms |
29012 KB |
Output is correct |
12 |
Correct |
47 ms |
29032 KB |
Output is correct |
13 |
Correct |
188 ms |
100904 KB |
Output is correct |
14 |
Correct |
164 ms |
69652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24148 KB |
Output is correct |
2 |
Correct |
13 ms |
24216 KB |
Output is correct |
3 |
Correct |
13 ms |
24092 KB |
Output is correct |
4 |
Correct |
12 ms |
24100 KB |
Output is correct |
5 |
Correct |
12 ms |
24020 KB |
Output is correct |
6 |
Correct |
13 ms |
24020 KB |
Output is correct |
7 |
Correct |
14 ms |
24020 KB |
Output is correct |
8 |
Correct |
13 ms |
24020 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23820 KB |
Output is correct |
12 |
Correct |
13 ms |
24532 KB |
Output is correct |
13 |
Correct |
13 ms |
24148 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
24060 KB |
Output is correct |
16 |
Correct |
13 ms |
24068 KB |
Output is correct |
17 |
Correct |
12 ms |
24040 KB |
Output is correct |
18 |
Correct |
11 ms |
23784 KB |
Output is correct |
19 |
Correct |
11 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
60184 KB |
Output is correct |
2 |
Correct |
157 ms |
59524 KB |
Output is correct |
3 |
Correct |
12 ms |
23792 KB |
Output is correct |
4 |
Correct |
55 ms |
30360 KB |
Output is correct |
5 |
Correct |
100 ms |
45376 KB |
Output is correct |
6 |
Correct |
142 ms |
47232 KB |
Output is correct |
7 |
Correct |
120 ms |
47672 KB |
Output is correct |
8 |
Correct |
151 ms |
46884 KB |
Output is correct |
9 |
Correct |
317 ms |
53416 KB |
Output is correct |
10 |
Correct |
222 ms |
51216 KB |
Output is correct |
11 |
Correct |
289 ms |
50676 KB |
Output is correct |
12 |
Correct |
294 ms |
46352 KB |
Output is correct |
13 |
Correct |
212 ms |
46784 KB |
Output is correct |
14 |
Correct |
209 ms |
46912 KB |
Output is correct |
15 |
Correct |
207 ms |
47068 KB |
Output is correct |
16 |
Correct |
191 ms |
47136 KB |
Output is correct |
17 |
Correct |
58 ms |
27728 KB |
Output is correct |
18 |
Correct |
82 ms |
29968 KB |
Output is correct |
19 |
Correct |
155 ms |
60108 KB |
Output is correct |
20 |
Correct |
146 ms |
47668 KB |
Output is correct |
21 |
Correct |
112 ms |
46552 KB |
Output is correct |
22 |
Correct |
285 ms |
50136 KB |
Output is correct |
23 |
Correct |
402 ms |
50008 KB |
Output is correct |
24 |
Correct |
245 ms |
53944 KB |
Output is correct |
25 |
Correct |
231 ms |
54260 KB |
Output is correct |
26 |
Correct |
210 ms |
54108 KB |
Output is correct |
27 |
Correct |
195 ms |
54100 KB |
Output is correct |
28 |
Correct |
59 ms |
28824 KB |
Output is correct |
29 |
Correct |
45 ms |
29012 KB |
Output is correct |
30 |
Correct |
47 ms |
29032 KB |
Output is correct |
31 |
Correct |
188 ms |
100904 KB |
Output is correct |
32 |
Correct |
164 ms |
69652 KB |
Output is correct |
33 |
Correct |
13 ms |
24148 KB |
Output is correct |
34 |
Correct |
13 ms |
24216 KB |
Output is correct |
35 |
Correct |
13 ms |
24092 KB |
Output is correct |
36 |
Correct |
12 ms |
24100 KB |
Output is correct |
37 |
Correct |
12 ms |
24020 KB |
Output is correct |
38 |
Correct |
13 ms |
24020 KB |
Output is correct |
39 |
Correct |
14 ms |
24020 KB |
Output is correct |
40 |
Correct |
13 ms |
24020 KB |
Output is correct |
41 |
Correct |
12 ms |
23764 KB |
Output is correct |
42 |
Correct |
12 ms |
23764 KB |
Output is correct |
43 |
Correct |
11 ms |
23820 KB |
Output is correct |
44 |
Correct |
13 ms |
24532 KB |
Output is correct |
45 |
Correct |
13 ms |
24148 KB |
Output is correct |
46 |
Correct |
12 ms |
23764 KB |
Output is correct |
47 |
Correct |
13 ms |
24060 KB |
Output is correct |
48 |
Correct |
13 ms |
24068 KB |
Output is correct |
49 |
Correct |
12 ms |
24040 KB |
Output is correct |
50 |
Correct |
11 ms |
23784 KB |
Output is correct |
51 |
Correct |
11 ms |
23892 KB |
Output is correct |
52 |
Correct |
152 ms |
64996 KB |
Output is correct |
53 |
Correct |
143 ms |
50936 KB |
Output is correct |
54 |
Correct |
110 ms |
49788 KB |
Output is correct |
55 |
Correct |
268 ms |
55148 KB |
Output is correct |
56 |
Correct |
360 ms |
55584 KB |
Output is correct |
57 |
Correct |
241 ms |
54076 KB |
Output is correct |
58 |
Correct |
222 ms |
54124 KB |
Output is correct |
59 |
Correct |
208 ms |
54132 KB |
Output is correct |
60 |
Correct |
73 ms |
29256 KB |
Output is correct |
61 |
Correct |
51 ms |
29024 KB |
Output is correct |
62 |
Correct |
48 ms |
29056 KB |
Output is correct |
63 |
Correct |
193 ms |
101108 KB |
Output is correct |
64 |
Correct |
164 ms |
68312 KB |
Output is correct |
65 |
Correct |
51 ms |
29520 KB |
Output is correct |
66 |
Correct |
304 ms |
52260 KB |
Output is correct |
67 |
Correct |
222 ms |
52508 KB |
Output is correct |
68 |
Correct |
212 ms |
52296 KB |
Output is correct |
69 |
Correct |
51 ms |
28868 KB |
Output is correct |
70 |
Correct |
127 ms |
39728 KB |
Output is correct |