#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
//#define int long long
#define all(x) x.begin(), x.end()
#define endl '\n'
const int N=500005;
int n,m;
vector<int> vis(N,-1),out(N,0);
vector<int> adj[N];
int32_t main() {
LCBorz;
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b;cin>>a>>b;
out[a]++;
adj[b].push_back(a);
}
vector<int> ans1(N,-1);
queue<int> q;
for(int i=1;i<=n;i++){
if(out[i]==0){
ans1[i]=1;
q.push(i);
}
}
auto run=[&]()->void {
while(q.size()){
int p=q.front();
q.pop();
ans1[p]=1;
for(int j:adj[p]){
if(ans1[j]==1){
cout<<-1<<endl;
exit(0);
}
if(ans1[j]==-1){
for(int j1:adj[j]){
out[j1]--;
if(out[j1]==0){
q.push(j1);
}
}
}
ans1[j]=0;
}
}
};
run();
for(int i=1;i<=n;i++){
if(ans1[i]==-1){
q.push(i);
run();
}
}
vector<int> ans;
for(int i=1;i<=n;i++){
if(ans1[i]==1){
ans.push_back(i);
}
}
cout<<ans.size()<<endl;
for(int j:ans){
cout<<j<<' ';
}
cout<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
39280 KB |
Output is correct |
2 |
Correct |
108 ms |
38852 KB |
Output is correct |
3 |
Correct |
8 ms |
18012 KB |
Output is correct |
4 |
Correct |
40 ms |
25124 KB |
Output is correct |
5 |
Correct |
67 ms |
31312 KB |
Output is correct |
6 |
Correct |
101 ms |
41536 KB |
Output is correct |
7 |
Correct |
62 ms |
22216 KB |
Output is correct |
8 |
Correct |
98 ms |
35012 KB |
Output is correct |
9 |
Correct |
172 ms |
32144 KB |
Output is correct |
10 |
Correct |
132 ms |
38920 KB |
Output is correct |
11 |
Correct |
164 ms |
35144 KB |
Output is correct |
12 |
Correct |
180 ms |
33176 KB |
Output is correct |
13 |
Correct |
143 ms |
33356 KB |
Output is correct |
14 |
Correct |
133 ms |
33464 KB |
Output is correct |
15 |
Correct |
127 ms |
33404 KB |
Output is correct |
16 |
Correct |
118 ms |
33476 KB |
Output is correct |
17 |
Correct |
35 ms |
20820 KB |
Output is correct |
18 |
Correct |
59 ms |
22616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
39128 KB |
Output is correct |
2 |
Correct |
106 ms |
42000 KB |
Output is correct |
3 |
Correct |
58 ms |
22220 KB |
Output is correct |
4 |
Correct |
172 ms |
34884 KB |
Output is correct |
5 |
Correct |
212 ms |
35096 KB |
Output is correct |
6 |
Correct |
157 ms |
34288 KB |
Output is correct |
7 |
Correct |
147 ms |
37304 KB |
Output is correct |
8 |
Correct |
140 ms |
37192 KB |
Output is correct |
9 |
Correct |
127 ms |
37196 KB |
Output is correct |
10 |
Correct |
39 ms |
21784 KB |
Output is correct |
11 |
Incorrect |
30 ms |
21072 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
18292 KB |
Output is correct |
2 |
Correct |
9 ms |
18268 KB |
Output is correct |
3 |
Correct |
9 ms |
17980 KB |
Output is correct |
4 |
Correct |
10 ms |
18268 KB |
Output is correct |
5 |
Correct |
9 ms |
18268 KB |
Output is correct |
6 |
Correct |
9 ms |
18268 KB |
Output is correct |
7 |
Correct |
9 ms |
18268 KB |
Output is correct |
8 |
Correct |
8 ms |
18012 KB |
Output is correct |
9 |
Correct |
8 ms |
17940 KB |
Output is correct |
10 |
Incorrect |
8 ms |
18012 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
39280 KB |
Output is correct |
2 |
Correct |
108 ms |
38852 KB |
Output is correct |
3 |
Correct |
8 ms |
18012 KB |
Output is correct |
4 |
Correct |
40 ms |
25124 KB |
Output is correct |
5 |
Correct |
67 ms |
31312 KB |
Output is correct |
6 |
Correct |
101 ms |
41536 KB |
Output is correct |
7 |
Correct |
62 ms |
22216 KB |
Output is correct |
8 |
Correct |
98 ms |
35012 KB |
Output is correct |
9 |
Correct |
172 ms |
32144 KB |
Output is correct |
10 |
Correct |
132 ms |
38920 KB |
Output is correct |
11 |
Correct |
164 ms |
35144 KB |
Output is correct |
12 |
Correct |
180 ms |
33176 KB |
Output is correct |
13 |
Correct |
143 ms |
33356 KB |
Output is correct |
14 |
Correct |
133 ms |
33464 KB |
Output is correct |
15 |
Correct |
127 ms |
33404 KB |
Output is correct |
16 |
Correct |
118 ms |
33476 KB |
Output is correct |
17 |
Correct |
35 ms |
20820 KB |
Output is correct |
18 |
Correct |
59 ms |
22616 KB |
Output is correct |
19 |
Correct |
102 ms |
39128 KB |
Output is correct |
20 |
Correct |
106 ms |
42000 KB |
Output is correct |
21 |
Correct |
58 ms |
22220 KB |
Output is correct |
22 |
Correct |
172 ms |
34884 KB |
Output is correct |
23 |
Correct |
212 ms |
35096 KB |
Output is correct |
24 |
Correct |
157 ms |
34288 KB |
Output is correct |
25 |
Correct |
147 ms |
37304 KB |
Output is correct |
26 |
Correct |
140 ms |
37192 KB |
Output is correct |
27 |
Correct |
127 ms |
37196 KB |
Output is correct |
28 |
Correct |
39 ms |
21784 KB |
Output is correct |
29 |
Incorrect |
30 ms |
21072 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |