forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3222 - Edge Pairing.cpp
74 lines (55 loc) · 1.42 KB
/
3222 - Edge Pairing.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define MAXN 20001
#define MAXE 200000
int E = 0,to[MAXE],nxt[MAXE],last[MAXN];
void add_edge(int u, int v){
to[E] = v; nxt[E] = last[u]; last[u] = E++;
to[E] = u; nxt[E] = last[v]; last[v] = E++;
}
int parent[MAXN],level[MAXN];
vector<int> in[MAXN];
void dfs(int pos, int lvl){
level[pos] = lvl;
bool back = true;
for(int e = last[pos];e != -1;e = nxt[e]){
int x = to[e];
if(x == parent[pos]){
if(!back) in[x].push_back(pos);
back = false;
}else if(parent[x] != -1){
if(level[x] < lvl)
in[x].push_back(pos);
}else{
parent[x] = pos;
dfs(x,lvl + 1);
}
}
int sz = in[pos].size();
bool odd = false;
for(int i = 0;i < sz;i += 2){
if(i + 1 < sz){
printf("%d %d %d\n",in[pos][i],pos,in[pos][i + 1]);
}else{
odd = true;
printf("%d %d %d\n",in[pos][i],pos,parent[pos]);
}
}
if(!odd)
in[ parent[pos] ].push_back(pos);
}
int main(){
int n,m;
scanf("%d %d",&n,&m);
memset(last,-1,sizeof last);
for(int i = 0,u,v;i < m;++i){
scanf("%d %d",&u,&v);
add_edge(u,v);
}
memset(parent,-1,sizeof parent);
parent[1] = 0;
dfs(1,0);
return 0;
}