-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMail Delivery.cpp
68 lines (62 loc) · 1.28 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
63
64
65
66
67
68
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define ll long long
#define pii pair<int,int>
#define endl '\n'
#define mod 1000000007
const int N = 100005;
//assumes G is connected undirected simple Graph
set<int> G[N];
vector<int> sol;
void etour(int node)
{
stack<int> dStack;
dStack.push(node);
while(!dStack.empty())
{
int tnode=dStack.top();
if(G[tnode].empty())
{
sol.push_back(tnode);
dStack.pop();
}
else{
int v=*G[tnode].begin();
dStack.push(v);
G[tnode].erase(v);
G[v].erase(tnode);
}
}
}
void solve(){
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
G[u].insert(v);
G[v].insert(u);
}
for(int i=1;i<=n;i++){
if( G[i].size()%2 ){
cout<<"IMPOSSIBLE"<<endl;
return ;
}
}
etour(1);
if( sol.size() != m+1 ){
cout<<"IMPOSSIBLE"<<endl;
return ;
}
for(auto e:sol) cout<<e<<" ";
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
int tc=1;
//cin>>tc;
for(int i=0;i<tc;i++){
solve();
}
}