#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define keish ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int n, m, u, v;
signed main(){
keish;
cin >> n >> m;
vector<vector<int>> e(n);
vector<int> deg(n);
for(int i = 0; i < m; i++){
cin >> u >> v; u--, v--;
e[v].push_back(u);
deg[u]++;
}
queue<int> q;
vector<int> ans;
for(int i = 0; i < n; i++){
if(!deg[i]){
q.push(i);
ans.push_back(i);
}
}
while(!q.empty()){
int u = q.front(); q.pop();
for(auto v : e[u]){
deg[v]--;
if(!deg[v]){
q.push(v);
ans.push_back(v);
}
}
}
for(int i = 0; i < n; i++){
if(deg[i]){
cout << -1 << '\n';
return 0;
}
}
cout << ans.size() << '\n';
for(auto x : ans) cout << x + 1 << " " << 1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
17292 KB |
Output is correct |
2 |
Correct |
75 ms |
17364 KB |
Output is correct |
3 |
Correct |
41 ms |
9152 KB |
Output is correct |
4 |
Correct |
80 ms |
17336 KB |
Output is correct |
5 |
Correct |
82 ms |
17340 KB |
Output is correct |
6 |
Correct |
83 ms |
17376 KB |
Output is correct |
7 |
Correct |
82 ms |
17340 KB |
Output is correct |
8 |
Correct |
60 ms |
13572 KB |
Output is correct |
9 |
Correct |
83 ms |
17268 KB |
Output is correct |
10 |
Correct |
81 ms |
17316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
82 ms |
17292 KB |
Output is correct |
16 |
Correct |
75 ms |
17364 KB |
Output is correct |
17 |
Correct |
41 ms |
9152 KB |
Output is correct |
18 |
Correct |
80 ms |
17336 KB |
Output is correct |
19 |
Correct |
82 ms |
17340 KB |
Output is correct |
20 |
Correct |
83 ms |
17376 KB |
Output is correct |
21 |
Correct |
82 ms |
17340 KB |
Output is correct |
22 |
Correct |
60 ms |
13572 KB |
Output is correct |
23 |
Correct |
83 ms |
17268 KB |
Output is correct |
24 |
Correct |
81 ms |
17316 KB |
Output is correct |
25 |
Correct |
86 ms |
16008 KB |
Output is correct |
26 |
Correct |
74 ms |
17324 KB |
Output is correct |
27 |
Correct |
87 ms |
18504 KB |
Output is correct |
28 |
Correct |
112 ms |
20964 KB |
Output is correct |
29 |
Correct |
67 ms |
17244 KB |
Output is correct |
30 |
Correct |
147 ms |
20720 KB |
Output is correct |
31 |
Correct |
16 ms |
3028 KB |
Output is correct |
32 |
Correct |
84 ms |
19756 KB |
Output is correct |
33 |
Correct |
143 ms |
23840 KB |
Output is correct |
34 |
Correct |
98 ms |
19892 KB |
Output is correct |
35 |
Correct |
152 ms |
23920 KB |
Output is correct |
36 |
Correct |
83 ms |
19568 KB |
Output is correct |
37 |
Correct |
131 ms |
22700 KB |
Output is correct |
38 |
Correct |
116 ms |
21432 KB |
Output is correct |
39 |
Correct |
140 ms |
23716 KB |
Output is correct |
40 |
Correct |
140 ms |
22132 KB |
Output is correct |
41 |
Correct |
139 ms |
22076 KB |
Output is correct |
42 |
Correct |
155 ms |
24536 KB |
Output is correct |
43 |
Correct |
104 ms |
20328 KB |
Output is correct |
44 |
Correct |
156 ms |
24732 KB |
Output is correct |
45 |
Correct |
97 ms |
19896 KB |
Output is correct |
46 |
Correct |
147 ms |
22292 KB |
Output is correct |