-
Notifications
You must be signed in to change notification settings - Fork 4
/
Teleporters_Path.cpp
111 lines (77 loc) · 1.96 KB
/
Teleporters_Path.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include<bits/stdc++.h>
#include <iostream>
#include<vector>
using namespace std;
int v1[2000000];
int v2[2000000];
vector<int>res;
int vin[2000000];
int vout[2000000];
vector<int>adj[2000000];
int vis[2000000];
void dfs(int u)
{
while(adj[u].size())
{
int e=adj[u].back();
adj[u].pop_back();
if(vis[e])
continue;
vis[e]=1;
dfs(v1[e]^v2[e]^u);
res.push_back(u);
}
}
int main()
{
int m,n;
cin>>n>>m;
for(int i=0;i<m;i++)
{
cin>>v1[i]>>v2[i];
adj[v1[i]].push_back(i);
//adj[v2[i]].push_back(i);
vin[v2[i]]+=1;
vout[v1[i]]+=1;
}
int oc=0;
for(int i=1;i<n+1;i++)
{
if(i==1 && vin[1]+1!=vout[1])
{
cout<<"IMPOSSIBLE";
return 0;
}
else if(i==n && vout[n]+1!=vin[n])
{
cout<<"IMPOSSIBLE";
return 0;
}
else if((i!=1 && i!=n) && vin[i]!=vout[i])
{
cout<<"IMPOSSIBLE";
return 0;
}
}
// res.push_back(1);
dfs(1);
if(res.size()^m)
{
cout<<"IMPOSSIBLE";
return 0;
}
reverse(res.begin(),res.end());
//cout<<"1 ";
for(int nm : res)
{
cout<<nm<<" ";
}
cout<<n;
// cout<<endl;
return 0;
}