-
Notifications
You must be signed in to change notification settings - Fork 161
/
Even Outdegree Edges.cpp
55 lines (48 loc) · 1.22 KB
/
Even Outdegree Edges.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int maxN = 1e5+5, maxM = 2e5+5;
int N, M, timer, in[maxN];
bool even[maxN];
vector<pii> G[maxN];
pii ans[maxM];
void dfs(int u, int p = -1){
in[u] = ++timer;
for(pii e : G[u]){
int v = e.first, id = e.second;
if(v != p){
if(!in[v]){
dfs(v, u);
if(even[v]){
ans[id] = {u, v};
even[u] ^= true;
} else {
ans[id] = {v, u};
even[v] ^= true;
}
} else if(in[v] < in[u]){
even[u] ^= true;
ans[id] = {u, v};
}
}
}
}
int main(){
scanf("%d %d", &N, &M);
for(int i = 0, a, b; i < M; i++){
scanf("%d %d", &a, &b);
G[a].push_back({b, i});
G[b].push_back({a, i});
}
fill(even+1, even+N+1, true);
for(int i = 1; i <= N; i++)
if(!in[i])
dfs(i);
bool good = true;
for(int i = 1; i <= N; i++)
good &= even[i];
if(good){
for(int i = 0; i < M; i++)
printf("%d %d\n", ans[i].first, ans[i].second);
} else printf("IMPOSSIBLE\n");
}