#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less_equal<>,
rb_tree_tag, tree_order_statistics_node_update>
ordered_set;
#define ll long long
#define ii pair<ll,ll>
#define endl '\n'
#define cerr if (0) cerr
const ll inf=1e15;
const ll maxn=2e5+5;
const ll mod=1e9+7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll cnt[maxn][2];
bool vis[maxn];
set<ii> adj[maxn];
queue<ii> q;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n,m,a,b;
cin>>n>>m;
for (int i=0;i<m;i++){
cin>>a>>b;
adj[a].emplace(b,0);
adj[b].emplace(a,1);
cnt[a][0]++;
cnt[b][1]++;
}
for (int i=1;i<=n;i++){
cerr<<cnt[i][0]<<' '<<cnt[i][1]<<endl;
if (cnt[i][0]==0){
q.emplace(i,0);
vis[i]=1;
} else if (cnt[i][1]==0){
q.emplace(i,1);
vis[i]=1;
}
}
stack<ii> s;
while (q.size()){
tie(a,b)=q.front();
cerr<<a<<' '<<b<<'x'<<endl;
q.pop();
for (auto [x,y]:adj[a]){
cerr<<a<<' '<<x<<' '<<y<<endl;
adj[x].erase(make_pair(a,b));
if (cnt[x][b]==1 && vis[x]==0){
q.emplace(x,b);
vis[x]=1;
}
cnt[x][b]--;
}
s.emplace(a,b);
}
for (int i=1;i<=n;i++){
cerr<<cnt[i][0]<<' '<<cnt[i][1]<<'y'<<endl;
if (cnt[i][0] && cnt[i][1]){
cout<<-1;
return 0;
}
}
cout<<s.size()<<endl;
while (s.size()){
cout<<s.top().first<<' '<<s.top().second<<endl;
s.pop();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
5 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 |
9656 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9724 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9648 KB |
Output is correct |
13 |
Correct |
5 ms |
9728 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
43124 KB |
Output is correct |
2 |
Correct |
280 ms |
45632 KB |
Output is correct |
3 |
Correct |
159 ms |
28476 KB |
Output is correct |
4 |
Correct |
310 ms |
45580 KB |
Output is correct |
5 |
Correct |
293 ms |
45768 KB |
Output is correct |
6 |
Correct |
289 ms |
45576 KB |
Output is correct |
7 |
Correct |
298 ms |
45712 KB |
Output is correct |
8 |
Correct |
211 ms |
37700 KB |
Output is correct |
9 |
Correct |
286 ms |
45544 KB |
Output is correct |
10 |
Correct |
319 ms |
45652 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 |
5 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 |
9656 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9724 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9648 KB |
Output is correct |
13 |
Correct |
5 ms |
9728 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
278 ms |
43124 KB |
Output is correct |
16 |
Correct |
280 ms |
45632 KB |
Output is correct |
17 |
Correct |
159 ms |
28476 KB |
Output is correct |
18 |
Correct |
310 ms |
45580 KB |
Output is correct |
19 |
Correct |
293 ms |
45768 KB |
Output is correct |
20 |
Correct |
289 ms |
45576 KB |
Output is correct |
21 |
Correct |
298 ms |
45712 KB |
Output is correct |
22 |
Correct |
211 ms |
37700 KB |
Output is correct |
23 |
Correct |
286 ms |
45544 KB |
Output is correct |
24 |
Correct |
319 ms |
45652 KB |
Output is correct |
25 |
Correct |
489 ms |
68072 KB |
Output is correct |
26 |
Correct |
399 ms |
56584 KB |
Output is correct |
27 |
Correct |
500 ms |
65580 KB |
Output is correct |
28 |
Correct |
618 ms |
78772 KB |
Output is correct |
29 |
Correct |
337 ms |
50124 KB |
Output is correct |
30 |
Correct |
580 ms |
77540 KB |
Output is correct |
31 |
Correct |
77 ms |
21408 KB |
Output is correct |
32 |
Correct |
336 ms |
49220 KB |
Output is correct |
33 |
Correct |
772 ms |
79920 KB |
Output is correct |
34 |
Correct |
367 ms |
51784 KB |
Output is correct |
35 |
Correct |
775 ms |
80564 KB |
Output is correct |
36 |
Correct |
330 ms |
48112 KB |
Output is correct |
37 |
Correct |
691 ms |
73576 KB |
Output is correct |
38 |
Correct |
563 ms |
64536 KB |
Output is correct |
39 |
Correct |
768 ms |
79020 KB |
Output is correct |
40 |
Correct |
623 ms |
69860 KB |
Output is correct |
41 |
Correct |
644 ms |
69148 KB |
Output is correct |
42 |
Correct |
856 ms |
83720 KB |
Output is correct |
43 |
Correct |
421 ms |
55116 KB |
Output is correct |
44 |
Correct |
869 ms |
85216 KB |
Output is correct |
45 |
Correct |
367 ms |
51624 KB |
Output is correct |
46 |
Correct |
635 ms |
69912 KB |
Output is correct |