-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathRound Trip
More file actions
83 lines (61 loc) · 1.26 KB
/
Copy pathRound Trip
File metadata and controls
83 lines (61 loc) · 1.26 KB
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
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define S second
#define F first
#define f(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
const int N = 1e5+10;
vi g[N];
vector<bool> vis(N,0);
vector<int> par(N,-1);
void dfs(int node,int p)
{
par[node] = p;
vis[node] = 1;
for(auto x : g[node])
if(!vis[x]) dfs(x,node);
else if(x != p)
{
vector<int> cyc;
int cur = node;
cyc.pb(node);
while(par[cur] != x)
{
cur = par[cur];
cyc.pb(cur);
}
cyc.pb(x);
cyc.pb(node);
cout << cyc.size() << '\n';
for(auto x : cyc) cout << x <<" ";
exit(0);
}
}
void solve()
{
int n,m;
cin >> n >> m;
int a,b;
f(i,m)
{
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
for(int i=1;i<=n;i++)
if(!vis[i])
dfs(i,-1);
cout<<"IMPOSSIBLE\n";
}
signed main()
{
fast;
int t = 1;
// cin >> t;
while(t--)
solve();
}