#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second
queue<int> q;
vector<int> child[200005], ans;
int par[200005];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
/*
1 2
2 3
3 4
urutan penarikan = 4 -> 3 -> 2 -> 1
urutan pengeboman = 1 -> 2 -> 3 -> 4
3 3
1 2
2 3
1 3
3 -> 2 -> 1
3 -> 1
*/
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
child[u].push_back(v);
par[v]++;
}
for (int i = 1; i <= n; i++) if (par[i] == 0) q.push(i);
while (!q.empty()) {
int x = q.front();
q.pop();
ans.push_back(x);
for (auto u : child[x]) {
par[u]--;
if (par[u] == 0) q.push(u);
}
}
for (int i = 1; i <= n; i++) {
if (par[i] != 0) {
cout << -1 << "\n";
return 0;
}
}
cout << (int)ans.size() << "\n";
for (auto x : ans) cout << x << " 0" << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5024 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5032 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 KB |
Output is correct |
13 |
Correct |
3 ms |
5028 KB |
Output is correct |
14 |
Correct |
2 ms |
5020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
13912 KB |
Output is correct |
2 |
Correct |
71 ms |
13980 KB |
Output is correct |
3 |
Correct |
33 ms |
10248 KB |
Output is correct |
4 |
Correct |
80 ms |
13972 KB |
Output is correct |
5 |
Correct |
83 ms |
13972 KB |
Output is correct |
6 |
Correct |
84 ms |
14012 KB |
Output is correct |
7 |
Correct |
73 ms |
13960 KB |
Output is correct |
8 |
Correct |
55 ms |
12272 KB |
Output is correct |
9 |
Correct |
76 ms |
13932 KB |
Output is correct |
10 |
Correct |
70 ms |
14028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5024 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5032 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 KB |
Output is correct |
13 |
Correct |
3 ms |
5028 KB |
Output is correct |
14 |
Correct |
2 ms |
5020 KB |
Output is correct |
15 |
Correct |
72 ms |
13912 KB |
Output is correct |
16 |
Correct |
71 ms |
13980 KB |
Output is correct |
17 |
Correct |
33 ms |
10248 KB |
Output is correct |
18 |
Correct |
80 ms |
13972 KB |
Output is correct |
19 |
Correct |
83 ms |
13972 KB |
Output is correct |
20 |
Correct |
84 ms |
14012 KB |
Output is correct |
21 |
Correct |
73 ms |
13960 KB |
Output is correct |
22 |
Correct |
55 ms |
12272 KB |
Output is correct |
23 |
Correct |
76 ms |
13932 KB |
Output is correct |
24 |
Correct |
70 ms |
14028 KB |
Output is correct |
25 |
Correct |
92 ms |
12092 KB |
Output is correct |
26 |
Correct |
90 ms |
14132 KB |
Output is correct |
27 |
Correct |
95 ms |
13596 KB |
Output is correct |
28 |
Correct |
115 ms |
13900 KB |
Output is correct |
29 |
Correct |
62 ms |
13176 KB |
Output is correct |
30 |
Correct |
104 ms |
13852 KB |
Output is correct |
31 |
Correct |
17 ms |
6860 KB |
Output is correct |
32 |
Correct |
73 ms |
13312 KB |
Output is correct |
33 |
Correct |
146 ms |
15120 KB |
Output is correct |
34 |
Correct |
85 ms |
13480 KB |
Output is correct |
35 |
Correct |
146 ms |
16180 KB |
Output is correct |
36 |
Correct |
70 ms |
13664 KB |
Output is correct |
37 |
Correct |
116 ms |
15120 KB |
Output is correct |
38 |
Correct |
98 ms |
15368 KB |
Output is correct |
39 |
Correct |
143 ms |
16144 KB |
Output is correct |
40 |
Correct |
105 ms |
15624 KB |
Output is correct |
41 |
Correct |
105 ms |
15548 KB |
Output is correct |
42 |
Correct |
143 ms |
16264 KB |
Output is correct |
43 |
Correct |
79 ms |
14728 KB |
Output is correct |
44 |
Correct |
134 ms |
16140 KB |
Output is correct |
45 |
Correct |
81 ms |
14288 KB |
Output is correct |
46 |
Correct |
109 ms |
15400 KB |
Output is correct |