#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);
}
}
while(q.size()){
int p=q.front();
q.pop();
ans1[p]=1;
for(int j:adj[p]){
if(ans1[j]==1){
cout<<-1<<endl;
return 0;
}
if(ans1[j]==-1){
for(int j1:adj[j]){
out[j1]--;
if(out[j1]==0){
q.push(j1);
}
}
}
ans1[j]=0;
}
}
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 |
100 ms |
39120 KB |
Output is correct |
2 |
Correct |
106 ms |
41740 KB |
Output is correct |
3 |
Correct |
7 ms |
18012 KB |
Output is correct |
4 |
Correct |
45 ms |
25136 KB |
Output is correct |
5 |
Correct |
65 ms |
32824 KB |
Output is correct |
6 |
Correct |
105 ms |
43164 KB |
Output is correct |
7 |
Correct |
58 ms |
24380 KB |
Output is correct |
8 |
Correct |
100 ms |
36548 KB |
Output is correct |
9 |
Correct |
170 ms |
35272 KB |
Output is correct |
10 |
Correct |
140 ms |
41752 KB |
Output is correct |
11 |
Correct |
164 ms |
38036 KB |
Output is correct |
12 |
Correct |
183 ms |
35920 KB |
Output is correct |
13 |
Correct |
139 ms |
36228 KB |
Output is correct |
14 |
Correct |
134 ms |
36432 KB |
Output is correct |
15 |
Correct |
128 ms |
36308 KB |
Output is correct |
16 |
Correct |
125 ms |
36360 KB |
Output is correct |
17 |
Correct |
33 ms |
21584 KB |
Output is correct |
18 |
Correct |
58 ms |
24364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
39328 KB |
Output is correct |
2 |
Correct |
107 ms |
43704 KB |
Output is correct |
3 |
Correct |
56 ms |
24008 KB |
Output is correct |
4 |
Correct |
169 ms |
37660 KB |
Output is correct |
5 |
Correct |
226 ms |
38264 KB |
Output is correct |
6 |
Incorrect |
155 ms |
37192 KB |
For each person outside the committee there should be someone in the committee who they dislike. |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
18264 KB |
Output is correct |
2 |
Correct |
7 ms |
18268 KB |
Output is correct |
3 |
Correct |
6 ms |
18012 KB |
Output is correct |
4 |
Correct |
7 ms |
18012 KB |
Output is correct |
5 |
Incorrect |
7 ms |
18012 KB |
For each person outside the committee there should be someone in the committee who they dislike. |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
39120 KB |
Output is correct |
2 |
Correct |
106 ms |
41740 KB |
Output is correct |
3 |
Correct |
7 ms |
18012 KB |
Output is correct |
4 |
Correct |
45 ms |
25136 KB |
Output is correct |
5 |
Correct |
65 ms |
32824 KB |
Output is correct |
6 |
Correct |
105 ms |
43164 KB |
Output is correct |
7 |
Correct |
58 ms |
24380 KB |
Output is correct |
8 |
Correct |
100 ms |
36548 KB |
Output is correct |
9 |
Correct |
170 ms |
35272 KB |
Output is correct |
10 |
Correct |
140 ms |
41752 KB |
Output is correct |
11 |
Correct |
164 ms |
38036 KB |
Output is correct |
12 |
Correct |
183 ms |
35920 KB |
Output is correct |
13 |
Correct |
139 ms |
36228 KB |
Output is correct |
14 |
Correct |
134 ms |
36432 KB |
Output is correct |
15 |
Correct |
128 ms |
36308 KB |
Output is correct |
16 |
Correct |
125 ms |
36360 KB |
Output is correct |
17 |
Correct |
33 ms |
21584 KB |
Output is correct |
18 |
Correct |
58 ms |
24364 KB |
Output is correct |
19 |
Correct |
99 ms |
39328 KB |
Output is correct |
20 |
Correct |
107 ms |
43704 KB |
Output is correct |
21 |
Correct |
56 ms |
24008 KB |
Output is correct |
22 |
Correct |
169 ms |
37660 KB |
Output is correct |
23 |
Correct |
226 ms |
38264 KB |
Output is correct |
24 |
Incorrect |
155 ms |
37192 KB |
For each person outside the committee there should be someone in the committee who they dislike. |
25 |
Halted |
0 ms |
0 KB |
- |