#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
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 |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
108 ms |
20572 KB |
Output is correct |
2 |
Correct |
125 ms |
20488 KB |
Output is correct |
3 |
Correct |
41 ms |
10952 KB |
Output is correct |
4 |
Correct |
125 ms |
20652 KB |
Output is correct |
5 |
Correct |
91 ms |
20640 KB |
Output is correct |
6 |
Correct |
112 ms |
20492 KB |
Output is correct |
7 |
Correct |
115 ms |
20672 KB |
Output is correct |
8 |
Correct |
89 ms |
16068 KB |
Output is correct |
9 |
Correct |
93 ms |
20512 KB |
Output is correct |
10 |
Correct |
108 ms |
20508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
452 KB |
Output is correct |
15 |
Correct |
108 ms |
20572 KB |
Output is correct |
16 |
Correct |
125 ms |
20488 KB |
Output is correct |
17 |
Correct |
41 ms |
10952 KB |
Output is correct |
18 |
Correct |
125 ms |
20652 KB |
Output is correct |
19 |
Correct |
91 ms |
20640 KB |
Output is correct |
20 |
Correct |
112 ms |
20492 KB |
Output is correct |
21 |
Correct |
115 ms |
20672 KB |
Output is correct |
22 |
Correct |
89 ms |
16068 KB |
Output is correct |
23 |
Correct |
93 ms |
20512 KB |
Output is correct |
24 |
Correct |
108 ms |
20508 KB |
Output is correct |
25 |
Correct |
111 ms |
21288 KB |
Output is correct |
26 |
Correct |
112 ms |
22736 KB |
Output is correct |
27 |
Correct |
153 ms |
24264 KB |
Output is correct |
28 |
Correct |
178 ms |
27700 KB |
Output is correct |
29 |
Correct |
125 ms |
20840 KB |
Output is correct |
30 |
Correct |
157 ms |
27296 KB |
Output is correct |
31 |
Correct |
18 ms |
4184 KB |
Output is correct |
32 |
Correct |
106 ms |
21080 KB |
Output is correct |
33 |
Correct |
207 ms |
29656 KB |
Output is correct |
34 |
Correct |
124 ms |
21704 KB |
Output is correct |
35 |
Correct |
189 ms |
29648 KB |
Output is correct |
36 |
Correct |
140 ms |
20864 KB |
Output is correct |
37 |
Correct |
172 ms |
27904 KB |
Output is correct |
38 |
Correct |
136 ms |
25288 KB |
Output is correct |
39 |
Correct |
190 ms |
29392 KB |
Output is correct |
40 |
Correct |
204 ms |
26720 KB |
Output is correct |
41 |
Correct |
152 ms |
26600 KB |
Output is correct |
42 |
Correct |
191 ms |
30656 KB |
Output is correct |
43 |
Correct |
148 ms |
22624 KB |
Output is correct |
44 |
Correct |
202 ms |
30904 KB |
Output is correct |
45 |
Correct |
130 ms |
21676 KB |
Output is correct |
46 |
Correct |
154 ms |
26868 KB |
Output is correct |