#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define all(x) x.begin(),x.end()
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n,m;
cin>>n>>m;
vector<int> inout(n,0),deg(n,0);
vector<bool> visited(n,false);
vector<int> adj[n],adjr[n];
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
a--;
b--;
inout[a]--;
inout[b]++;
deg[a]++;
deg[b]++;
adj[b].push_back(a);
adjr[a].push_back(b);
}
queue<int> q;
for(int i=0;i<n;i++){
if(deg[i]==abs(inout[i])){
visited[i]=true;
q.push(i);
}
}
vector<pair<int,int>> ans;
while(!q.empty()){
int v=q.front();
q.pop();
ans.push_back({v+1,(inout[v]<0?1:0)});
for(auto u:adj[v]){
//if(visited[u]) continue;
inout[u]++;
deg[u]--;
if(deg[u]==abs(inout[u])){
if(!visited[u]){
visited[u]=true;
q.push(u);
}
}
}
for(auto u:adjr[v]){
//if(visited[u]) continue;
inout[u]--;
deg[u]--;
if(deg[u]==abs(inout[u])){
if(!visited[u]){
visited[u]=true;
q.push(u);
}
}
}
}
for(int i=0;i<n;i++){
if(deg[i]>0){
cout<<-1;
return 0;
}
}
reverse(all(ans));
for(auto v:ans){
cout<<v.f<<' '<<v.s<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Incorrect |
0 ms |
212 KB |
Integer parameter [name=c] equals to 0, violates the range [1, 10] |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
222 ms |
23552 KB |
Integer parameter [name=c] equals to 0, violates the range [1, 200000] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Incorrect |
0 ms |
212 KB |
Integer parameter [name=c] equals to 0, violates the range [1, 10] |
9 |
Halted |
0 ms |
0 KB |
- |