#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll LOGN = 20;
const ll MAXN = 2e5 + 5;
vector<vector<pair<int,int>>> graph;
int indeg[MAXN], outdeg[MAXN], vis[MAXN];
int main() {
fast
int n, m, a, b;
cin >> n >> m;
graph = vector<vector<pair<int,int>>>(n+1, vector<pair<int,int>>());
for (int i = 0; i < m; i++) {
cin >> a >> b;
graph[a].push_back({b, 0});
graph[b].push_back({a, 1});
indeg[a]++;
outdeg[b]++;
}
queue<int> q;
for (int i = 1; i <= n; i++) {
if (indeg[i] == 0 || outdeg[i] == 0)
q.push(i);
}
stack<pair<int,int>> ans;
while (!q.empty()) {
int node = q.front();
q.pop();
if (vis[node])
continue;
vis[node] = true;
int state = -1;
for (auto u : graph[node]) {
if (vis[u.f])
continue;
if (u.s == 1)
indeg[u.f]--;
else
outdeg[u.f]--;
if (indeg[u.f] == 0 || outdeg[u.f] == 0)
q.push(u.f);
state = u.s;
}
if (state != -1)
ans.push({node, !state});
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
cout << "-1\n";
return 0;
}
}
cout << ans.size() << "\n";
while (!ans.empty()) {
cout << ans.top().f << " " << ans.top().s << "\n";
ans.pop();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2592 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
19536 KB |
Output is correct |
2 |
Correct |
95 ms |
19268 KB |
Output is correct |
3 |
Correct |
40 ms |
11304 KB |
Output is correct |
4 |
Correct |
104 ms |
19120 KB |
Output is correct |
5 |
Correct |
91 ms |
19280 KB |
Output is correct |
6 |
Correct |
90 ms |
19104 KB |
Output is correct |
7 |
Correct |
89 ms |
19280 KB |
Output is correct |
8 |
Correct |
64 ms |
15444 KB |
Output is correct |
9 |
Correct |
88 ms |
19280 KB |
Output is correct |
10 |
Correct |
96 ms |
19124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2592 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
107 ms |
19536 KB |
Output is correct |
16 |
Correct |
95 ms |
19268 KB |
Output is correct |
17 |
Correct |
40 ms |
11304 KB |
Output is correct |
18 |
Correct |
104 ms |
19120 KB |
Output is correct |
19 |
Correct |
91 ms |
19280 KB |
Output is correct |
20 |
Correct |
90 ms |
19104 KB |
Output is correct |
21 |
Correct |
89 ms |
19280 KB |
Output is correct |
22 |
Correct |
64 ms |
15444 KB |
Output is correct |
23 |
Correct |
88 ms |
19280 KB |
Output is correct |
24 |
Correct |
96 ms |
19124 KB |
Output is correct |
25 |
Correct |
105 ms |
20304 KB |
Output is correct |
26 |
Correct |
103 ms |
20308 KB |
Output is correct |
27 |
Correct |
137 ms |
22352 KB |
Output is correct |
28 |
Correct |
167 ms |
25424 KB |
Output is correct |
29 |
Correct |
89 ms |
18932 KB |
Output is correct |
30 |
Correct |
160 ms |
24912 KB |
Output is correct |
31 |
Correct |
18 ms |
6232 KB |
Output is correct |
32 |
Correct |
125 ms |
20656 KB |
Output is correct |
33 |
Correct |
179 ms |
27472 KB |
Output is correct |
34 |
Correct |
122 ms |
20756 KB |
Output is correct |
35 |
Correct |
181 ms |
27472 KB |
Output is correct |
36 |
Correct |
95 ms |
20048 KB |
Output is correct |
37 |
Correct |
162 ms |
25940 KB |
Output is correct |
38 |
Correct |
162 ms |
23796 KB |
Output is correct |
39 |
Correct |
219 ms |
27472 KB |
Output is correct |
40 |
Correct |
161 ms |
24912 KB |
Output is correct |
41 |
Correct |
157 ms |
24912 KB |
Output is correct |
42 |
Correct |
240 ms |
28240 KB |
Output is correct |
43 |
Correct |
130 ms |
21840 KB |
Output is correct |
44 |
Correct |
298 ms |
28852 KB |
Output is correct |
45 |
Correct |
116 ms |
20840 KB |
Output is correct |
46 |
Correct |
153 ms |
25120 KB |
Output is correct |