#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 2e5 + 5;
int N, M;
int deg[MX];
vector<int> g[MX];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> N >> M;
for(int i = 0; i < M; i++) {
int u, v;
cin >> u >> v;
g[v].push_back(u);
deg[u]++;
}
queue<int> q;
vector<int> ord;
for(int i = 1; i <= N; i++) {
if(!deg[i]) {
q.push(i);
}
}
int cnt = 0;
while(!q.empty()) {
auto v = q.front(); q.pop();
cnt++;
for(auto u : g[v]) {
deg[u]--;
if(!deg[u]) {
q.push(u);
ord.push_back(u);
}
}
}
if(cnt < N) {
cout << -1 << '\n';
return 0;
}
cout << ord.size() << '\n';
for(auto x : ord) {
cout << x << " " << 1 << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 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 |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5004 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5028 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
12044 KB |
Output is correct |
2 |
Correct |
79 ms |
13552 KB |
Output is correct |
3 |
Correct |
34 ms |
9932 KB |
Output is correct |
4 |
Correct |
66 ms |
13568 KB |
Output is correct |
5 |
Correct |
66 ms |
13596 KB |
Output is correct |
6 |
Correct |
76 ms |
13636 KB |
Output is correct |
7 |
Correct |
89 ms |
13560 KB |
Output is correct |
8 |
Correct |
51 ms |
11972 KB |
Output is correct |
9 |
Correct |
65 ms |
13516 KB |
Output is correct |
10 |
Correct |
69 ms |
13512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 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 |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5004 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5028 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
66 ms |
12044 KB |
Output is correct |
16 |
Correct |
79 ms |
13552 KB |
Output is correct |
17 |
Correct |
34 ms |
9932 KB |
Output is correct |
18 |
Correct |
66 ms |
13568 KB |
Output is correct |
19 |
Correct |
66 ms |
13596 KB |
Output is correct |
20 |
Correct |
76 ms |
13636 KB |
Output is correct |
21 |
Correct |
89 ms |
13560 KB |
Output is correct |
22 |
Correct |
51 ms |
11972 KB |
Output is correct |
23 |
Correct |
65 ms |
13516 KB |
Output is correct |
24 |
Correct |
69 ms |
13512 KB |
Output is correct |
25 |
Correct |
82 ms |
11604 KB |
Output is correct |
26 |
Correct |
109 ms |
12844 KB |
Output is correct |
27 |
Correct |
104 ms |
13084 KB |
Output is correct |
28 |
Correct |
135 ms |
13472 KB |
Output is correct |
29 |
Correct |
85 ms |
12772 KB |
Output is correct |
30 |
Correct |
126 ms |
13404 KB |
Output is correct |
31 |
Correct |
17 ms |
6828 KB |
Output is correct |
32 |
Correct |
67 ms |
15348 KB |
Output is correct |
33 |
Correct |
147 ms |
16004 KB |
Output is correct |
34 |
Correct |
92 ms |
15376 KB |
Output is correct |
35 |
Correct |
151 ms |
16012 KB |
Output is correct |
36 |
Correct |
75 ms |
15428 KB |
Output is correct |
37 |
Correct |
167 ms |
15776 KB |
Output is correct |
38 |
Correct |
122 ms |
15624 KB |
Output is correct |
39 |
Correct |
155 ms |
16152 KB |
Output is correct |
40 |
Correct |
105 ms |
15816 KB |
Output is correct |
41 |
Correct |
105 ms |
15776 KB |
Output is correct |
42 |
Correct |
137 ms |
16052 KB |
Output is correct |
43 |
Correct |
135 ms |
15456 KB |
Output is correct |
44 |
Correct |
159 ms |
16120 KB |
Output is correct |
45 |
Correct |
100 ms |
15408 KB |
Output is correct |
46 |
Correct |
132 ms |
15788 KB |
Output is correct |