#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ii pair<ll,ll>
#define fi first
#define se second
#define endl '\n'
#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound
#define rep(x,s,e) for (auto x=(s)-((s)>(e));x!=(e)-((s)>(e));((s)<(e)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,m;
vector<ii> al[200005];
int in[200005];
int out[200005];
bool vis[200005];
vector<int> v;
vector<ii> ans;
signed main(){
cin.tie(0);
cout.tie(0);
cin.sync_with_stdio(false);
cin>>n>>m;
int a,b;
rep(x,0,m){
cin>>a>>b;
al[a].pub({b,0});
al[b].pub({a,1});
in[b]++;
out[a]++;
}
rep(x,1,n+1) if (in[x]==0 || out[x]==0){
vis[x]=true;
v.pub(x);
}
while (!v.empty()){
int u=v.back(); v.pob();
if (in[u]==0) ans.pub({u,1});
else ans.pub({u,0});
for (auto [it,x]:al[u]){
if (!vis[it]){
if (x==0) in[it]--;
else out[it]--;
if (in[it]==0 || out[it]==0){
vis[it]=true;
v.pub(it);
}
}
}
}
reverse(all(ans));
if (sz(ans)==n){
cout<<sz(ans)<<endl;
for (auto [a,b]:ans) cout<<a<<" "<<b<<endl;
}
else{
cout<<"-1"<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5032 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5024 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 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 |
5024 KB |
Output is correct |
14 |
Correct |
3 ms |
5028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
215 ms |
27060 KB |
Output is correct |
2 |
Correct |
198 ms |
27108 KB |
Output is correct |
3 |
Correct |
85 ms |
16616 KB |
Output is correct |
4 |
Correct |
235 ms |
27096 KB |
Output is correct |
5 |
Correct |
208 ms |
27136 KB |
Output is correct |
6 |
Correct |
234 ms |
27156 KB |
Output is correct |
7 |
Correct |
229 ms |
27180 KB |
Output is correct |
8 |
Correct |
163 ms |
22588 KB |
Output is correct |
9 |
Correct |
203 ms |
27088 KB |
Output is correct |
10 |
Correct |
207 ms |
27072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5032 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5024 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 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 |
5024 KB |
Output is correct |
14 |
Correct |
3 ms |
5028 KB |
Output is correct |
15 |
Correct |
215 ms |
27060 KB |
Output is correct |
16 |
Correct |
198 ms |
27108 KB |
Output is correct |
17 |
Correct |
85 ms |
16616 KB |
Output is correct |
18 |
Correct |
235 ms |
27096 KB |
Output is correct |
19 |
Correct |
208 ms |
27136 KB |
Output is correct |
20 |
Correct |
234 ms |
27156 KB |
Output is correct |
21 |
Correct |
229 ms |
27180 KB |
Output is correct |
22 |
Correct |
163 ms |
22588 KB |
Output is correct |
23 |
Correct |
203 ms |
27088 KB |
Output is correct |
24 |
Correct |
207 ms |
27072 KB |
Output is correct |
25 |
Correct |
288 ms |
33408 KB |
Output is correct |
26 |
Correct |
259 ms |
32344 KB |
Output is correct |
27 |
Correct |
343 ms |
34628 KB |
Output is correct |
28 |
Correct |
450 ms |
40116 KB |
Output is correct |
29 |
Correct |
244 ms |
29180 KB |
Output is correct |
30 |
Correct |
363 ms |
39412 KB |
Output is correct |
31 |
Correct |
33 ms |
10700 KB |
Output is correct |
32 |
Correct |
261 ms |
28428 KB |
Output is correct |
33 |
Correct |
429 ms |
42464 KB |
Output is correct |
34 |
Correct |
237 ms |
29696 KB |
Output is correct |
35 |
Correct |
412 ms |
42788 KB |
Output is correct |
36 |
Correct |
242 ms |
27972 KB |
Output is correct |
37 |
Correct |
414 ms |
39604 KB |
Output is correct |
38 |
Correct |
395 ms |
35552 KB |
Output is correct |
39 |
Correct |
441 ms |
42104 KB |
Output is correct |
40 |
Correct |
379 ms |
37820 KB |
Output is correct |
41 |
Correct |
356 ms |
37516 KB |
Output is correct |
42 |
Correct |
428 ms |
44312 KB |
Output is correct |
43 |
Correct |
279 ms |
31092 KB |
Output is correct |
44 |
Correct |
464 ms |
44960 KB |
Output is correct |
45 |
Correct |
269 ms |
29452 KB |
Output is correct |
46 |
Correct |
396 ms |
37816 KB |
Output is correct |