#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int n, m, c0[maxn], c1[maxn];
bool vis[maxn];
set<pair<int, int>> G[maxn];
vector<array<int, 2>> res;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
while (m--) {
int u, v;
cin >> u >> v;
G[u].emplace(v, 1), G[v].emplace(u, 0);
c0[u]++, c1[v]++;
}
queue<int> Q;
for (int i = 1; i <= n; i++) {
if (!c0[i] || !c1[i]) Q.push(i);
}
while (!Q.empty()) {
int x = Q.front(); Q.pop();
if (vis[x]) continue;
res.push_back({x, c0[x] ? 1 : 0}), vis[x] = 1;
for (auto &e : G[x]) if (!vis[e.first]) {
(e.second ? c1[e.first] : c0[e.first])--;
G[e.first].erase({x, !e.second});
if (!c0[e.first] || !c1[e.first]) Q.push(e.first);
}
}
if (count(vis + 1, vis + n + 1, 0)) cout << "-1\n", exit(0);
cout << res.size() << "\n";
reverse(res.begin(), res.end());
for (auto &p : res) {
cout << p[0] << " " << p[1] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9652 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9700 KB |
Output is correct |
11 |
Correct |
5 ms |
9732 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9700 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
277 ms |
34208 KB |
Output is correct |
2 |
Correct |
263 ms |
34204 KB |
Output is correct |
3 |
Correct |
121 ms |
22704 KB |
Output is correct |
4 |
Correct |
264 ms |
34232 KB |
Output is correct |
5 |
Correct |
262 ms |
34216 KB |
Output is correct |
6 |
Correct |
273 ms |
34400 KB |
Output is correct |
7 |
Correct |
266 ms |
34328 KB |
Output is correct |
8 |
Correct |
200 ms |
28984 KB |
Output is correct |
9 |
Correct |
270 ms |
34340 KB |
Output is correct |
10 |
Correct |
264 ms |
34376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9652 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9700 KB |
Output is correct |
11 |
Correct |
5 ms |
9732 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9700 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
277 ms |
34208 KB |
Output is correct |
16 |
Correct |
263 ms |
34204 KB |
Output is correct |
17 |
Correct |
121 ms |
22704 KB |
Output is correct |
18 |
Correct |
264 ms |
34232 KB |
Output is correct |
19 |
Correct |
262 ms |
34216 KB |
Output is correct |
20 |
Correct |
273 ms |
34400 KB |
Output is correct |
21 |
Correct |
266 ms |
34328 KB |
Output is correct |
22 |
Correct |
200 ms |
28984 KB |
Output is correct |
23 |
Correct |
270 ms |
34340 KB |
Output is correct |
24 |
Correct |
264 ms |
34376 KB |
Output is correct |
25 |
Correct |
477 ms |
49612 KB |
Output is correct |
26 |
Correct |
398 ms |
42132 KB |
Output is correct |
27 |
Correct |
469 ms |
47688 KB |
Output is correct |
28 |
Correct |
574 ms |
56924 KB |
Output is correct |
29 |
Correct |
315 ms |
37184 KB |
Output is correct |
30 |
Correct |
572 ms |
56072 KB |
Output is correct |
31 |
Correct |
67 ms |
17860 KB |
Output is correct |
32 |
Correct |
308 ms |
36536 KB |
Output is correct |
33 |
Correct |
721 ms |
57504 KB |
Output is correct |
34 |
Correct |
344 ms |
38336 KB |
Output is correct |
35 |
Correct |
726 ms |
57928 KB |
Output is correct |
36 |
Correct |
302 ms |
35856 KB |
Output is correct |
37 |
Correct |
634 ms |
53320 KB |
Output is correct |
38 |
Correct |
508 ms |
47380 KB |
Output is correct |
39 |
Correct |
707 ms |
57012 KB |
Output is correct |
40 |
Correct |
574 ms |
50748 KB |
Output is correct |
41 |
Correct |
566 ms |
50228 KB |
Output is correct |
42 |
Correct |
772 ms |
60084 KB |
Output is correct |
43 |
Correct |
386 ms |
40520 KB |
Output is correct |
44 |
Correct |
799 ms |
61048 KB |
Output is correct |
45 |
Correct |
342 ms |
38208 KB |
Output is correct |
46 |
Correct |
577 ms |
50720 KB |
Output is correct |