#include <bits/stdc++.h>
using namespace std;
# define int long long
# define fir first
# define sec second
# define pb push_back
const int cnst = 2e5+5;
bool mutipletestcase = 0;
//bool debug = false;
int x[cnst];
vector<int> vec[cnst];
void solve() {
int n, m; cin >> n >> m;
for(int i = 1; i<=m; i++) {
int a, b; cin >> a >> b;
vec[b].pb(a);
x[a]++;
}
queue<int> q;
int done = 0;
for(int i = 1; i<=n; i++) {
if(!x[i]) q.push(i), done++;
}
vector<pair<int, int>> ans;
while(!q.empty()) {
int a = q.front(); q.pop();
for(auto v: vec[a]) {
x[v]--;
if(!x[v]) {
q.push(v);
ans.pb({v, 1});
done++;
}
}
if(done == n) break;
}
if(done == n) {
cout << ans.size() << endl;
for(auto v: ans) cout << v.fir << " " << v.sec << endl;
}
else cout << -1 << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
int t = 1;
if(mutipletestcase) cin >> t;
while(t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4964 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5024 KB |
Output is correct |
8 |
Correct |
2 ms |
5028 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
5024 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
206 ms |
16888 KB |
Output is correct |
2 |
Correct |
197 ms |
16888 KB |
Output is correct |
3 |
Correct |
133 ms |
11312 KB |
Output is correct |
4 |
Correct |
204 ms |
17040 KB |
Output is correct |
5 |
Correct |
235 ms |
16944 KB |
Output is correct |
6 |
Correct |
202 ms |
16960 KB |
Output is correct |
7 |
Correct |
207 ms |
16968 KB |
Output is correct |
8 |
Correct |
152 ms |
13828 KB |
Output is correct |
9 |
Correct |
207 ms |
16904 KB |
Output is correct |
10 |
Correct |
201 ms |
16864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4964 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5024 KB |
Output is correct |
8 |
Correct |
2 ms |
5028 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
5024 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
206 ms |
16888 KB |
Output is correct |
16 |
Correct |
197 ms |
16888 KB |
Output is correct |
17 |
Correct |
133 ms |
11312 KB |
Output is correct |
18 |
Correct |
204 ms |
17040 KB |
Output is correct |
19 |
Correct |
235 ms |
16944 KB |
Output is correct |
20 |
Correct |
202 ms |
16960 KB |
Output is correct |
21 |
Correct |
207 ms |
16968 KB |
Output is correct |
22 |
Correct |
152 ms |
13828 KB |
Output is correct |
23 |
Correct |
207 ms |
16904 KB |
Output is correct |
24 |
Correct |
201 ms |
16864 KB |
Output is correct |
25 |
Correct |
130 ms |
13736 KB |
Output is correct |
26 |
Correct |
94 ms |
14708 KB |
Output is correct |
27 |
Correct |
108 ms |
14968 KB |
Output is correct |
28 |
Correct |
117 ms |
15976 KB |
Output is correct |
29 |
Correct |
72 ms |
15236 KB |
Output is correct |
30 |
Correct |
150 ms |
15820 KB |
Output is correct |
31 |
Correct |
19 ms |
6940 KB |
Output is correct |
32 |
Correct |
190 ms |
17324 KB |
Output is correct |
33 |
Correct |
299 ms |
20184 KB |
Output is correct |
34 |
Correct |
220 ms |
17408 KB |
Output is correct |
35 |
Correct |
308 ms |
20156 KB |
Output is correct |
36 |
Correct |
209 ms |
17272 KB |
Output is correct |
37 |
Correct |
268 ms |
19700 KB |
Output is correct |
38 |
Correct |
256 ms |
19344 KB |
Output is correct |
39 |
Correct |
295 ms |
20108 KB |
Output is correct |
40 |
Correct |
276 ms |
19492 KB |
Output is correct |
41 |
Correct |
277 ms |
19356 KB |
Output is correct |
42 |
Correct |
319 ms |
20432 KB |
Output is correct |
43 |
Correct |
198 ms |
17672 KB |
Output is correct |
44 |
Correct |
376 ms |
20584 KB |
Output is correct |
45 |
Correct |
196 ms |
17436 KB |
Output is correct |
46 |
Correct |
295 ms |
19504 KB |
Output is correct |