#include<bits/stdc++.h>
#define int long long
#define pii pair<int, int>
using namespace std;
const int MAX=1e18;
signed main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m; cin >> n >> m;
vector<vector<tuple<int, int, int>>> adj(n);
stack<pair<int, int>> dfs;
deque<pair<int, int>> ans;
vector<int> here(n), away(n);
vector<bool> used(m), vis(n);
for(int i=0; i<m; i++) {
int u, v; cin >> u >> v; --u; --v;
adj[u].push_back({v, 0, i});
adj[v].push_back({u, 1, i});
here[u]++; away[v]++;
}
for(int i=0; i<n; i++) {
if(here[i]==0 && away[i]==0) continue;
if(here[i]==0) dfs.push({i, 0});
if(away[i]==0) dfs.push({i, 1});
}
while(!dfs.empty()) {
int u=dfs.top().first, type=dfs.top().second;
dfs.pop(); vis[u]=1;
ans.push_front({u+1, type});
if(type) {
for(auto p : adj[u]) {
used[get<2>(p)]=1;
int pp=get<0>(p);
if(vis[pp]) continue;
away[pp]--;
if(away[pp]==0 && here[pp]==0) continue;
if(away[pp]==0) dfs.push({pp, 1}), used[get<2>(p)]=1;
}
} else {
for(auto p : adj[u]) {
used[get<2>(p)]=1;
int pp=get<0>(p);
if(vis[pp]) continue;
here[pp]--;
if(here[pp]==0 && away[pp]==0) continue;
if(here[pp]==0) dfs.push({pp, 0}), used[get<2>(p)]=1;
}
}
}
for(int i=0; i<m; i++) {
if(used[i]) continue;
cout << "-1\n"; return 0;
}
cout << ans.size() << '\n';
for(auto p : ans)
cout << p.first << ' ' << p.second << '\n';
return 0;
}
Compilation message
naboj.cpp: In function 'int main()':
naboj.cpp:54:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
54 | for(auto p : ans)
| ^~~
naboj.cpp:56:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
56 | return 0;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
28780 KB |
Output is correct |
2 |
Correct |
161 ms |
28800 KB |
Output is correct |
3 |
Correct |
51 ms |
15184 KB |
Output is correct |
4 |
Correct |
154 ms |
28704 KB |
Output is correct |
5 |
Correct |
140 ms |
28752 KB |
Output is correct |
6 |
Correct |
144 ms |
28700 KB |
Output is correct |
7 |
Correct |
137 ms |
28812 KB |
Output is correct |
8 |
Correct |
103 ms |
22416 KB |
Output is correct |
9 |
Correct |
152 ms |
28880 KB |
Output is correct |
10 |
Correct |
188 ms |
28768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
140 ms |
28780 KB |
Output is correct |
16 |
Correct |
161 ms |
28800 KB |
Output is correct |
17 |
Correct |
51 ms |
15184 KB |
Output is correct |
18 |
Correct |
154 ms |
28704 KB |
Output is correct |
19 |
Correct |
140 ms |
28752 KB |
Output is correct |
20 |
Correct |
144 ms |
28700 KB |
Output is correct |
21 |
Correct |
137 ms |
28812 KB |
Output is correct |
22 |
Correct |
103 ms |
22416 KB |
Output is correct |
23 |
Correct |
152 ms |
28880 KB |
Output is correct |
24 |
Correct |
188 ms |
28768 KB |
Output is correct |
25 |
Correct |
195 ms |
40664 KB |
Output is correct |
26 |
Correct |
201 ms |
35276 KB |
Output is correct |
27 |
Correct |
232 ms |
40796 KB |
Output is correct |
28 |
Correct |
247 ms |
49692 KB |
Output is correct |
29 |
Correct |
137 ms |
31184 KB |
Output is correct |
30 |
Correct |
331 ms |
48872 KB |
Output is correct |
31 |
Correct |
23 ms |
8444 KB |
Output is correct |
32 |
Correct |
184 ms |
31048 KB |
Output is correct |
33 |
Correct |
376 ms |
51756 KB |
Output is correct |
34 |
Correct |
204 ms |
32860 KB |
Output is correct |
35 |
Correct |
380 ms |
52232 KB |
Output is correct |
36 |
Correct |
194 ms |
30408 KB |
Output is correct |
37 |
Correct |
317 ms |
47568 KB |
Output is correct |
38 |
Correct |
266 ms |
41548 KB |
Output is correct |
39 |
Correct |
326 ms |
51192 KB |
Output is correct |
40 |
Correct |
274 ms |
45024 KB |
Output is correct |
41 |
Correct |
316 ms |
44432 KB |
Output is correct |
42 |
Correct |
403 ms |
54376 KB |
Output is correct |
43 |
Correct |
241 ms |
34952 KB |
Output is correct |
44 |
Correct |
364 ms |
55280 KB |
Output is correct |
45 |
Correct |
203 ms |
32640 KB |
Output is correct |
46 |
Correct |
298 ms |
45060 KB |
Output is correct |