-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathMail Delivery.cpp
62 lines (54 loc) · 1.38 KB
/
Mail Delivery.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
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int maxN = 1e5+1;
const int maxM = 2e5+1;
int N, M, deg[maxN];
bool tour_exists, used[maxM];
vector<pii> G[maxN];
vector<int> tour;
stack<int> S;
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});
deg[a]++; deg[b]++;
}
tour_exists = true;
for(int i = 1; i <= N; i++)
if(deg[i]&1)
tour_exists = false;
if(!tour_exists){
printf("IMPOSSIBLE\n");
return 0;
}
S.push(1);
while(!S.empty()){
int u = S.top();
if(deg[u]){
while(!G[u].empty()){
int v = G[u].back().first;
int id = G[u].back().second;
G[u].pop_back();
if(!used[id]){
deg[u]--; deg[v]--;
used[id] = true;
S.push(v);
break;
}
}
} else {
tour.push_back(u);
S.pop();
}
}
// Should be M+1 nodes in the Euler tour
// If not, it means the graph was not connected
if((int) tour.size() != M+1)
printf("IMPOSSIBLE\n");
else
for(int i = 0; i <= M; i++)
printf("%d%c", tour[i], (" \n")[i==M]);
}