#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
#define pb push_back
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
const int N = 2e5+5;
int n, m, in[N];
vector<int> adj[N];
vector<pair<int, int>> ans;
int cnt = 0;
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].pb(v);
in[v]++;
}
queue<int> q;
for (int i = 0; i < n; i++) {
if (!in[i])q.push(i);
}
while (!q.empty()) {
int u = q.front();
q.pop();
cnt++;
for (auto &v : adj[u]) {
if (--in[v] == 0)q.push(v);
}
if (adj[u].empty())ans.pb({u, 0});
else ans.pb({u, 1});
}
if (cnt != n) {
cout << -1;
return 0;
}
cout << sz(ans) << "\n";
reverse(all(ans));
for (auto &[x, y] : ans)cout << x+1 << " " << y << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4952 KB |
Output is correct |
6 |
Correct |
1 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
5208 KB |
Output is correct |
9 |
Correct |
1 ms |
4952 KB |
Output is correct |
10 |
Correct |
1 ms |
4952 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
1 ms |
4956 KB |
Output is correct |
13 |
Correct |
1 ms |
4952 KB |
Output is correct |
14 |
Correct |
2 ms |
5208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
13692 KB |
Output is correct |
2 |
Correct |
79 ms |
16188 KB |
Output is correct |
3 |
Correct |
35 ms |
10956 KB |
Output is correct |
4 |
Correct |
77 ms |
16316 KB |
Output is correct |
5 |
Correct |
90 ms |
16216 KB |
Output is correct |
6 |
Correct |
76 ms |
16324 KB |
Output is correct |
7 |
Correct |
78 ms |
16176 KB |
Output is correct |
8 |
Correct |
65 ms |
13720 KB |
Output is correct |
9 |
Correct |
75 ms |
16072 KB |
Output is correct |
10 |
Correct |
75 ms |
16376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4952 KB |
Output is correct |
6 |
Correct |
1 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
1 ms |
5208 KB |
Output is correct |
9 |
Correct |
1 ms |
4952 KB |
Output is correct |
10 |
Correct |
1 ms |
4952 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
1 ms |
4956 KB |
Output is correct |
13 |
Correct |
1 ms |
4952 KB |
Output is correct |
14 |
Correct |
2 ms |
5208 KB |
Output is correct |
15 |
Correct |
84 ms |
13692 KB |
Output is correct |
16 |
Correct |
79 ms |
16188 KB |
Output is correct |
17 |
Correct |
35 ms |
10956 KB |
Output is correct |
18 |
Correct |
77 ms |
16316 KB |
Output is correct |
19 |
Correct |
90 ms |
16216 KB |
Output is correct |
20 |
Correct |
76 ms |
16324 KB |
Output is correct |
21 |
Correct |
78 ms |
16176 KB |
Output is correct |
22 |
Correct |
65 ms |
13720 KB |
Output is correct |
23 |
Correct |
75 ms |
16072 KB |
Output is correct |
24 |
Correct |
75 ms |
16376 KB |
Output is correct |
25 |
Correct |
78 ms |
15184 KB |
Output is correct |
26 |
Correct |
68 ms |
15852 KB |
Output is correct |
27 |
Correct |
90 ms |
16700 KB |
Output is correct |
28 |
Correct |
98 ms |
18160 KB |
Output is correct |
29 |
Correct |
68 ms |
15968 KB |
Output is correct |
30 |
Correct |
96 ms |
18108 KB |
Output is correct |
31 |
Correct |
16 ms |
7004 KB |
Output is correct |
32 |
Correct |
80 ms |
15548 KB |
Output is correct |
33 |
Correct |
127 ms |
20296 KB |
Output is correct |
34 |
Correct |
91 ms |
16120 KB |
Output is correct |
35 |
Correct |
127 ms |
20184 KB |
Output is correct |
36 |
Correct |
88 ms |
15448 KB |
Output is correct |
37 |
Correct |
134 ms |
19360 KB |
Output is correct |
38 |
Correct |
104 ms |
17896 KB |
Output is correct |
39 |
Correct |
151 ms |
20192 KB |
Output is correct |
40 |
Correct |
111 ms |
18660 KB |
Output is correct |
41 |
Correct |
109 ms |
18540 KB |
Output is correct |
42 |
Correct |
131 ms |
20672 KB |
Output is correct |
43 |
Correct |
86 ms |
16444 KB |
Output is correct |
44 |
Correct |
150 ms |
21032 KB |
Output is correct |
45 |
Correct |
84 ms |
15868 KB |
Output is correct |
46 |
Correct |
128 ms |
18660 KB |
Output is correct |