#include<bits/stdc++.h>
using namespace std;
#define ioss ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
int n, m;
vector<int> adj[200004];
bool vis[200004];
int cnt[200004];
queue<int> q;
void toposort(int u) {
if(vis[u]) return;
q.push(u);
vis[u] = 1;
for(auto v : adj[u]) cnt[v]--;
for(auto v : adj[u]) {
if(!vis[v] && cnt[v] == 0) toposort(v);
}
}
int main() {
ioss;
cin >> n >> m;
set<int> tmp;
for(int i = 0; i < m; i++) {
int a, b; cin >> a >> b;
tmp.insert(a);
adj[b].pb(a);
cnt[a]++;
}
for(int i = 1; i <= n; i++) {
if(!vis[i] && cnt[i] == 0) toposort(i);
// cout << cnt[i] << endl;
}
bool ok = 1;
for(int i = 1; i <= n; i++) {
if(!vis[i]) {
ok = 0;
break;
}
}
if(!ok) {
cout << -1 << endl;
return 0;
}
cout << q.size() << endl;
while(!q.empty()) {
int u = q.front();
q.pop();
if(tmp.count(u)) cout << u << " " << 1 << endl;
else cout << u << " " << 0 << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
5024 KB |
Output is correct |
10 |
Correct |
2 ms |
5020 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
415 ms |
19628 KB |
Output is correct |
2 |
Correct |
420 ms |
19260 KB |
Output is correct |
3 |
Correct |
218 ms |
12544 KB |
Output is correct |
4 |
Correct |
512 ms |
19252 KB |
Output is correct |
5 |
Correct |
434 ms |
19268 KB |
Output is correct |
6 |
Correct |
508 ms |
19340 KB |
Output is correct |
7 |
Correct |
528 ms |
19248 KB |
Output is correct |
8 |
Correct |
336 ms |
16184 KB |
Output is correct |
9 |
Correct |
468 ms |
19232 KB |
Output is correct |
10 |
Correct |
452 ms |
19304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5012 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
5024 KB |
Output is correct |
10 |
Correct |
2 ms |
5020 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
415 ms |
19628 KB |
Output is correct |
16 |
Correct |
420 ms |
19260 KB |
Output is correct |
17 |
Correct |
218 ms |
12544 KB |
Output is correct |
18 |
Correct |
512 ms |
19252 KB |
Output is correct |
19 |
Correct |
434 ms |
19268 KB |
Output is correct |
20 |
Correct |
508 ms |
19340 KB |
Output is correct |
21 |
Correct |
528 ms |
19248 KB |
Output is correct |
22 |
Correct |
336 ms |
16184 KB |
Output is correct |
23 |
Correct |
468 ms |
19232 KB |
Output is correct |
24 |
Correct |
452 ms |
19304 KB |
Output is correct |
25 |
Correct |
260 ms |
16752 KB |
Output is correct |
26 |
Correct |
195 ms |
19404 KB |
Output is correct |
27 |
Correct |
232 ms |
20192 KB |
Output is correct |
28 |
Correct |
296 ms |
21072 KB |
Output is correct |
29 |
Correct |
177 ms |
18672 KB |
Output is correct |
30 |
Correct |
349 ms |
21052 KB |
Output is correct |
31 |
Correct |
29 ms |
7040 KB |
Output is correct |
32 |
Correct |
439 ms |
20304 KB |
Output is correct |
33 |
Correct |
567 ms |
22584 KB |
Output is correct |
34 |
Correct |
408 ms |
20600 KB |
Output is correct |
35 |
Correct |
680 ms |
22436 KB |
Output is correct |
36 |
Correct |
429 ms |
20188 KB |
Output is correct |
37 |
Correct |
531 ms |
22084 KB |
Output is correct |
38 |
Correct |
511 ms |
21568 KB |
Output is correct |
39 |
Correct |
600 ms |
22440 KB |
Output is correct |
40 |
Correct |
566 ms |
21952 KB |
Output is correct |
41 |
Correct |
556 ms |
21900 KB |
Output is correct |
42 |
Correct |
661 ms |
22704 KB |
Output is correct |
43 |
Correct |
444 ms |
20776 KB |
Output is correct |
44 |
Correct |
615 ms |
22692 KB |
Output is correct |
45 |
Correct |
505 ms |
20472 KB |
Output is correct |
46 |
Correct |
654 ms |
21924 KB |
Output is correct |