//記得跳題
//#pragma GCC optimize("O4,unroll_loops")
//#pragma GCC target("avx2")
#include<iostream>
#include<array>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<cstring>
#include<iomanip>
#include<bitset>
#include<tuple>
#include<random>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define pb push_back
#define INF (ll)(2e18)
#define F first
#define S second
#define endl "\n"
#define AC ios::sync_with_stdio(0);
using namespace std;
int n, m;
int in[200005];
vector<int> e[200005];
vector<pair<int, int> > ans;
signed main(){
AC;
cin >> n >> m;
FOR (i, 0, m) {
int a, b; cin >> a >> b;
in[b]++;
e[a].pb(b);
}
queue<int> Q;
FOR (i, 1, n+1) if (!in[i]) {
Q.push(i);
}
int cnt = 0;
while (Q.size()) {
int now = Q.front(); Q.pop();
ans.pb({now, 0});
cnt++;
for (auto i:e[now]) {
in[i]--;
if (!in[i]) {
Q.push(i);
}
}
}
if (cnt != n) {
cout << -1 << endl;
} else {
cout << ans.size() << endl;
for (auto i:ans) {
cout << i.F << " " << i.S << endl;
}
}
}
# |
Verdict |
Execution time |
Memory |
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 |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4920 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
13560 KB |
Output is correct |
2 |
Correct |
109 ms |
13716 KB |
Output is correct |
3 |
Correct |
58 ms |
9480 KB |
Output is correct |
4 |
Correct |
102 ms |
13576 KB |
Output is correct |
5 |
Correct |
103 ms |
13660 KB |
Output is correct |
6 |
Correct |
105 ms |
13648 KB |
Output is correct |
7 |
Correct |
98 ms |
13600 KB |
Output is correct |
8 |
Correct |
98 ms |
11656 KB |
Output is correct |
9 |
Correct |
104 ms |
13580 KB |
Output is correct |
10 |
Correct |
137 ms |
13592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4920 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
112 ms |
13560 KB |
Output is correct |
16 |
Correct |
109 ms |
13716 KB |
Output is correct |
17 |
Correct |
58 ms |
9480 KB |
Output is correct |
18 |
Correct |
102 ms |
13576 KB |
Output is correct |
19 |
Correct |
103 ms |
13660 KB |
Output is correct |
20 |
Correct |
105 ms |
13648 KB |
Output is correct |
21 |
Correct |
98 ms |
13600 KB |
Output is correct |
22 |
Correct |
98 ms |
11656 KB |
Output is correct |
23 |
Correct |
104 ms |
13580 KB |
Output is correct |
24 |
Correct |
137 ms |
13592 KB |
Output is correct |
25 |
Correct |
128 ms |
10712 KB |
Output is correct |
26 |
Correct |
133 ms |
12520 KB |
Output is correct |
27 |
Correct |
138 ms |
12468 KB |
Output is correct |
28 |
Correct |
167 ms |
12688 KB |
Output is correct |
29 |
Correct |
96 ms |
13260 KB |
Output is correct |
30 |
Correct |
171 ms |
12616 KB |
Output is correct |
31 |
Correct |
20 ms |
6084 KB |
Output is correct |
32 |
Correct |
118 ms |
13016 KB |
Output is correct |
33 |
Correct |
223 ms |
14892 KB |
Output is correct |
34 |
Correct |
125 ms |
13284 KB |
Output is correct |
35 |
Correct |
211 ms |
14956 KB |
Output is correct |
36 |
Correct |
131 ms |
13060 KB |
Output is correct |
37 |
Correct |
186 ms |
14564 KB |
Output is correct |
38 |
Correct |
158 ms |
14220 KB |
Output is correct |
39 |
Correct |
226 ms |
14896 KB |
Output is correct |
40 |
Correct |
204 ms |
14344 KB |
Output is correct |
41 |
Correct |
169 ms |
14388 KB |
Output is correct |
42 |
Correct |
202 ms |
15056 KB |
Output is correct |
43 |
Correct |
143 ms |
13640 KB |
Output is correct |
44 |
Correct |
230 ms |
15120 KB |
Output is correct |
45 |
Correct |
124 ms |
13360 KB |
Output is correct |
46 |
Correct |
197 ms |
14528 KB |
Output is correct |