#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define all(x) x.begin(),x.end()
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n,m;
cin>>n>>m;
vector<int> in(n,0);
vector<bool> visited(n,false);
vector<int> adj[n],adjr[n];
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
a--;
b--;
in[a]++;
adj[b].push_back(a);
}
queue<int> q;
for(int i=0;i<n;i++){
if(in[i]==0){
visited[i]=true;
q.push(i);
}
}
vector<pair<int,int>> ans;
while(!q.empty()){
int v=q.front();
q.pop();
ans.push_back({v,0});
for(auto u:adj[v]){
if(visited[u]){
cout<<-1;
return 0;
}
in[u]--;
if(in[u]==0){
visited[u]=true;
q.push(u);
}
}
}
if(ans.size()!=n){
cout<<-1;
return 0;
}
cout<<ans.size()<<'\n';
reverse(all(ans));
for(auto v:ans){
cout<<v.f+1<<' '<<v.s<<'\n';
}
}
Compilation message
naboj.cpp: In function 'int main()':
naboj.cpp:48:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
48 | if(ans.size()!=n){
| ~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
18396 KB |
Output is correct |
2 |
Correct |
118 ms |
19108 KB |
Output is correct |
3 |
Correct |
58 ms |
10668 KB |
Output is correct |
4 |
Correct |
115 ms |
19096 KB |
Output is correct |
5 |
Correct |
125 ms |
19136 KB |
Output is correct |
6 |
Correct |
118 ms |
19132 KB |
Output is correct |
7 |
Correct |
112 ms |
19140 KB |
Output is correct |
8 |
Correct |
96 ms |
15148 KB |
Output is correct |
9 |
Correct |
110 ms |
19132 KB |
Output is correct |
10 |
Correct |
115 ms |
19100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
108 ms |
18396 KB |
Output is correct |
16 |
Correct |
118 ms |
19108 KB |
Output is correct |
17 |
Correct |
58 ms |
10668 KB |
Output is correct |
18 |
Correct |
115 ms |
19096 KB |
Output is correct |
19 |
Correct |
125 ms |
19136 KB |
Output is correct |
20 |
Correct |
118 ms |
19132 KB |
Output is correct |
21 |
Correct |
112 ms |
19140 KB |
Output is correct |
22 |
Correct |
96 ms |
15148 KB |
Output is correct |
23 |
Correct |
110 ms |
19132 KB |
Output is correct |
24 |
Correct |
115 ms |
19100 KB |
Output is correct |
25 |
Correct |
175 ms |
12528 KB |
Output is correct |
26 |
Correct |
127 ms |
17668 KB |
Output is correct |
27 |
Correct |
157 ms |
17468 KB |
Output is correct |
28 |
Correct |
162 ms |
17644 KB |
Output is correct |
29 |
Correct |
107 ms |
18252 KB |
Output is correct |
30 |
Correct |
167 ms |
17520 KB |
Output is correct |
31 |
Correct |
21 ms |
2564 KB |
Output is correct |
32 |
Correct |
133 ms |
21232 KB |
Output is correct |
33 |
Correct |
272 ms |
21164 KB |
Output is correct |
34 |
Correct |
157 ms |
21228 KB |
Output is correct |
35 |
Correct |
237 ms |
21372 KB |
Output is correct |
36 |
Correct |
140 ms |
21384 KB |
Output is correct |
37 |
Correct |
216 ms |
21072 KB |
Output is correct |
38 |
Correct |
186 ms |
21176 KB |
Output is correct |
39 |
Correct |
223 ms |
21196 KB |
Output is correct |
40 |
Correct |
190 ms |
21244 KB |
Output is correct |
41 |
Correct |
217 ms |
21224 KB |
Output is correct |
42 |
Correct |
221 ms |
21332 KB |
Output is correct |
43 |
Correct |
155 ms |
21196 KB |
Output is correct |
44 |
Correct |
237 ms |
21308 KB |
Output is correct |
45 |
Correct |
164 ms |
21284 KB |
Output is correct |
46 |
Correct |
196 ms |
21176 KB |
Output is correct |