#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int,int>
#define fr first
#define sc second
using namespace std;
const int N = 2e5+5;
const int M = 5e5+5;
vector<int> adj[N];
vector<int> opp[N];
vector<int> marker;
vector<int> par;
vector<int> keep;
vector<bool> vis;
vector<int> v;
bool dfs(int x) {
marker[x] = 1;
for (auto item : adj[x]) {
if (!marker[item]) {
par[item] = x;
if (dfs(item)) return true;
} else if (marker[item] == 1) return true;
}
marker[x] = 2;
return false;
}
void dfs2(int x) {
vis[x] = 1;
for (auto item : adj[x]) {
if (!vis[item] and adj[item].size() and opp[item].size())
dfs2(item);
}
v.push_back(x);
}
signed main() {
int n, m; cin >> n >> m;
marker.assign(n+1, 0);
par.assign(n+1, -1);
vis.assign(n+1, 0);
for (int i = 0; i < m; i++) {
int a, b; cin >> a >> b;
adj[b].pb(a);
opp[a].pb(b);
}
for (int i = 1; i <= n; i++) {
if (!marker[i]) {
if (dfs(i)) {
cout << -1 << endl;
return 0;
}
}
}
vis.assign(n+1, 0);
for (int i = 1; i <= n; i++) {
if (!vis[i] and adj[i].size() and opp[i].size())
dfs2(i);
}
reverse(v.begin(), v.end());
cout << n << endl;
for (auto item : v) {
cout << item << " " << 1 << endl;
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
cout << i << " " << (adj[i].size() == 0) << endl;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 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 |
9696 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9696 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
389 ms |
24080 KB |
Output is correct |
2 |
Correct |
373 ms |
26612 KB |
Output is correct |
3 |
Correct |
185 ms |
18480 KB |
Output is correct |
4 |
Correct |
368 ms |
26548 KB |
Output is correct |
5 |
Correct |
381 ms |
26572 KB |
Output is correct |
6 |
Correct |
377 ms |
26524 KB |
Output is correct |
7 |
Correct |
399 ms |
26668 KB |
Output is correct |
8 |
Correct |
294 ms |
22852 KB |
Output is correct |
9 |
Correct |
369 ms |
26600 KB |
Output is correct |
10 |
Correct |
375 ms |
26584 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 |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 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 |
9696 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9696 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
389 ms |
24080 KB |
Output is correct |
16 |
Correct |
373 ms |
26612 KB |
Output is correct |
17 |
Correct |
185 ms |
18480 KB |
Output is correct |
18 |
Correct |
368 ms |
26548 KB |
Output is correct |
19 |
Correct |
381 ms |
26572 KB |
Output is correct |
20 |
Correct |
377 ms |
26524 KB |
Output is correct |
21 |
Correct |
399 ms |
26668 KB |
Output is correct |
22 |
Correct |
294 ms |
22852 KB |
Output is correct |
23 |
Correct |
369 ms |
26600 KB |
Output is correct |
24 |
Correct |
375 ms |
26584 KB |
Output is correct |
25 |
Correct |
248 ms |
29932 KB |
Output is correct |
26 |
Correct |
213 ms |
28024 KB |
Output is correct |
27 |
Correct |
251 ms |
30656 KB |
Output is correct |
28 |
Correct |
365 ms |
34896 KB |
Output is correct |
29 |
Correct |
183 ms |
26048 KB |
Output is correct |
30 |
Correct |
322 ms |
34532 KB |
Output is correct |
31 |
Correct |
44 ms |
13460 KB |
Output is correct |
32 |
Correct |
408 ms |
28668 KB |
Output is correct |
33 |
Correct |
572 ms |
36636 KB |
Output is correct |
34 |
Correct |
410 ms |
29356 KB |
Output is correct |
35 |
Correct |
570 ms |
36920 KB |
Output is correct |
36 |
Correct |
395 ms |
28472 KB |
Output is correct |
37 |
Correct |
529 ms |
34724 KB |
Output is correct |
38 |
Correct |
491 ms |
32444 KB |
Output is correct |
39 |
Correct |
589 ms |
36448 KB |
Output is correct |
40 |
Correct |
497 ms |
33856 KB |
Output is correct |
41 |
Correct |
504 ms |
33528 KB |
Output is correct |
42 |
Correct |
628 ms |
37704 KB |
Output is correct |
43 |
Correct |
426 ms |
30148 KB |
Output is correct |
44 |
Correct |
617 ms |
38016 KB |
Output is correct |
45 |
Correct |
410 ms |
29376 KB |
Output is correct |
46 |
Correct |
552 ms |
33732 KB |
Output is correct |