This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
void solve(){
int n,m;
cin>>n>>m;
vector<int> pos(n+1) , neg(n+1);
vector<vector<pair<int,int>>> adj(n+1);
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
adj[a].push_back({b,1});
adj[b].push_back({a,0});
pos[a] ++;
neg[b] ++;
}
queue<int> q;
vector<bool> vis(n+1);
for(int i=1;i<=n;i++){
if(pos[i] && neg[i]) continue;
q.push(i);
vis[i] = 1;
}
vector<pair<int,int>> ans;
while(q.size()){
int cur = q.front(); q.pop();
bool curt = 0;
if(pos[cur]) ans.push_back({cur,1}) , curt = 1;
if(neg[cur]) ans.push_back({cur,0}) , curt = 0;
pos[cur] = neg[cur] = 0;
for(auto [ch,t] : adj[cur]){
if(vis[ch]) continue;
if(curt != t) return cout<<-1<<endl , void();
if(t) neg[ch] --;
else pos[ch] --;
if(pos[ch] == 0 || neg[ch] == 0){
q.push(ch);
vis[ch] = 1;
}
}
}
for(int i=1;i<=n;i++){
if(pos[i] || neg[i]) return cout<<-1<<endl, void();
}
reverse(ans.begin(),ans.end());
cout<<ans.size()<<endl;
for(auto [i,t] : ans) cout<<i<<' '<<t<<endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
Compilation message (stderr)
naboj.cpp: In function 'void solve()':
naboj.cpp:32:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
32 | for(auto [ch,t] : adj[cur]){
| ^
naboj.cpp:48:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
48 | for(auto [i,t] : ans) cout<<i<<' '<<t<<endl;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |