#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define endl "\n"
#define pii pair<ll,ll>
#define pb push_back
#define vi vector<ll>
#define pque priority_queue
#define pqueg priority_queue<ll,vector<ll>,greater<ll>>
#define que queue<ll>
#define FOR(m,i,n) for(int i=(m); i<=(n); i++)
#define FORM(m,i,n) for(int i=(m); i>=(n); i--)
ll n,m,u,v;
bool vis[200200];
vector<ll> adj[200200],ans;
ll top[200200];
queue<ll> q;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
FOR(1,i,m) {
cin >> u >> v;
adj[v].pb(u);
top[u]++;
}
FOR(1,i,n) {
if(top[i] == 0) {
q.push(i);
vis[i] = true;
ans.pb(i);
}
}
while(!q.empty()) {
ll x = q.front();
q.pop();
for(auto i : adj[x]) {
if(!vis[i]) {
top[i]--;
if(top[i] == 0) {
vis[i] = true;
ans.pb(i);
q.push(i);
}
}
}
}
if(ans.size() != n) {
cout << -1 << endl;
return 0;
}
cout << n << endl;
for(auto i : ans) {
cout << i << " 1" << endl;
}
}
/*
5 5
3 1
2 3
4 2
5 2
5 4
*/
Compilation message
naboj.cpp: In function 'int main()':
naboj.cpp:50:17: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
50 | if(ans.size() != n) {
| ~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
5036 KB |
Output is correct |
3 |
Correct |
3 ms |
5040 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
16320 KB |
Output is correct |
2 |
Correct |
84 ms |
16508 KB |
Output is correct |
3 |
Correct |
35 ms |
11048 KB |
Output is correct |
4 |
Correct |
70 ms |
16624 KB |
Output is correct |
5 |
Correct |
73 ms |
16576 KB |
Output is correct |
6 |
Correct |
71 ms |
16572 KB |
Output is correct |
7 |
Correct |
80 ms |
16572 KB |
Output is correct |
8 |
Correct |
55 ms |
14264 KB |
Output is correct |
9 |
Correct |
76 ms |
16576 KB |
Output is correct |
10 |
Correct |
74 ms |
16564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
5036 KB |
Output is correct |
3 |
Correct |
3 ms |
5040 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
73 ms |
16320 KB |
Output is correct |
16 |
Correct |
84 ms |
16508 KB |
Output is correct |
17 |
Correct |
35 ms |
11048 KB |
Output is correct |
18 |
Correct |
70 ms |
16624 KB |
Output is correct |
19 |
Correct |
73 ms |
16576 KB |
Output is correct |
20 |
Correct |
71 ms |
16572 KB |
Output is correct |
21 |
Correct |
80 ms |
16572 KB |
Output is correct |
22 |
Correct |
55 ms |
14264 KB |
Output is correct |
23 |
Correct |
76 ms |
16576 KB |
Output is correct |
24 |
Correct |
74 ms |
16564 KB |
Output is correct |
25 |
Correct |
104 ms |
15232 KB |
Output is correct |
26 |
Correct |
98 ms |
16196 KB |
Output is correct |
27 |
Correct |
96 ms |
16412 KB |
Output is correct |
28 |
Correct |
116 ms |
17440 KB |
Output is correct |
29 |
Correct |
80 ms |
16380 KB |
Output is correct |
30 |
Correct |
137 ms |
17344 KB |
Output is correct |
31 |
Correct |
18 ms |
7184 KB |
Output is correct |
32 |
Correct |
86 ms |
18760 KB |
Output is correct |
33 |
Correct |
146 ms |
20664 KB |
Output is correct |
34 |
Correct |
96 ms |
19000 KB |
Output is correct |
35 |
Correct |
167 ms |
20772 KB |
Output is correct |
36 |
Correct |
78 ms |
18620 KB |
Output is correct |
37 |
Correct |
148 ms |
20168 KB |
Output is correct |
38 |
Correct |
146 ms |
19760 KB |
Output is correct |
39 |
Correct |
153 ms |
20608 KB |
Output is correct |
40 |
Correct |
137 ms |
19944 KB |
Output is correct |
41 |
Correct |
127 ms |
20024 KB |
Output is correct |
42 |
Correct |
175 ms |
21028 KB |
Output is correct |
43 |
Correct |
91 ms |
19372 KB |
Output is correct |
44 |
Correct |
150 ms |
21068 KB |
Output is correct |
45 |
Correct |
92 ms |
19024 KB |
Output is correct |
46 |
Correct |
119 ms |
19976 KB |
Output is correct |