#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], y[cnst];
int dis[cnst];
vector<int> vec[cnst], vec2[cnst];
// void dfs(int x) {
// for(auto v: vec[x]) {
// if(dis[v] != 0) continue;
// dis[v] = dis[x]+1;
// dfs(v);
// }
// }
void solve() {
int n, m; cin >> n >> m;
for(int i = 1; i<=m; i++) {
int a, b; cin >> a >> b;
vec[a].pb(b);
vec2[b].pb(a);
x[a]++; y[b]++;
}
// bool yes = 0;
// for(int i = 1; i<=n; i++) if(!y[i]) yes = 1;
// if(!yes) {cout << "-1" << endl; return;}
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: vec2[a]) {
x[v]--;
if(!x[v]) {
q.push(v);
ans.pb({v, 1});
done++;
}
}
// cerr << a << " " << done << endl;
// if(done != n) ans.pb({a, 0});
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 |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 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 |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9728 KB |
Output is correct |
12 |
Correct |
5 ms |
9720 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
243 ms |
26556 KB |
Output is correct |
2 |
Correct |
279 ms |
29004 KB |
Output is correct |
3 |
Correct |
122 ms |
19784 KB |
Output is correct |
4 |
Correct |
257 ms |
29052 KB |
Output is correct |
5 |
Correct |
273 ms |
29016 KB |
Output is correct |
6 |
Correct |
248 ms |
28984 KB |
Output is correct |
7 |
Correct |
275 ms |
28960 KB |
Output is correct |
8 |
Correct |
183 ms |
24012 KB |
Output is correct |
9 |
Correct |
247 ms |
29072 KB |
Output is correct |
10 |
Correct |
295 ms |
28988 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 |
9684 KB |
Output is correct |
4 |
Correct |
4 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 |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9728 KB |
Output is correct |
12 |
Correct |
5 ms |
9720 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
243 ms |
26556 KB |
Output is correct |
16 |
Correct |
279 ms |
29004 KB |
Output is correct |
17 |
Correct |
122 ms |
19784 KB |
Output is correct |
18 |
Correct |
257 ms |
29052 KB |
Output is correct |
19 |
Correct |
273 ms |
29016 KB |
Output is correct |
20 |
Correct |
248 ms |
28984 KB |
Output is correct |
21 |
Correct |
275 ms |
28960 KB |
Output is correct |
22 |
Correct |
183 ms |
24012 KB |
Output is correct |
23 |
Correct |
247 ms |
29072 KB |
Output is correct |
24 |
Correct |
295 ms |
28988 KB |
Output is correct |
25 |
Correct |
165 ms |
30016 KB |
Output is correct |
26 |
Correct |
175 ms |
29428 KB |
Output is correct |
27 |
Correct |
208 ms |
31568 KB |
Output is correct |
28 |
Correct |
271 ms |
35248 KB |
Output is correct |
29 |
Correct |
143 ms |
28608 KB |
Output is correct |
30 |
Correct |
243 ms |
34792 KB |
Output is correct |
31 |
Correct |
37 ms |
13496 KB |
Output is correct |
32 |
Correct |
237 ms |
29544 KB |
Output is correct |
33 |
Correct |
395 ms |
38168 KB |
Output is correct |
34 |
Correct |
255 ms |
30260 KB |
Output is correct |
35 |
Correct |
401 ms |
38444 KB |
Output is correct |
36 |
Correct |
232 ms |
29320 KB |
Output is correct |
37 |
Correct |
364 ms |
36544 KB |
Output is correct |
38 |
Correct |
339 ms |
34328 KB |
Output is correct |
39 |
Correct |
387 ms |
37992 KB |
Output is correct |
40 |
Correct |
357 ms |
35600 KB |
Output is correct |
41 |
Correct |
345 ms |
35336 KB |
Output is correct |
42 |
Correct |
414 ms |
39368 KB |
Output is correct |
43 |
Correct |
293 ms |
31048 KB |
Output is correct |
44 |
Correct |
441 ms |
39668 KB |
Output is correct |
45 |
Correct |
283 ms |
30188 KB |
Output is correct |
46 |
Correct |
386 ms |
35544 KB |
Output is correct |