-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRound Trip.cpp
More file actions
88 lines (72 loc) · 1.75 KB
/
Round Trip.cpp
File metadata and controls
88 lines (72 loc) · 1.75 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
84
85
86
87
88
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,n) for (int i = 1; i <= n; i++)
#define mod 1000000007
using vi = vector<int>;
#define pb push_back
bool dfs(vector<int> adj[], vector<bool>& vis,int node,vector<int>& ans,int par){
vis[node] = true;
ans.push_back(node);
for(auto k:adj[node]){
if(!vis[k]){
if(dfs(adj,vis,k,ans,node)) return true;
}
else if(par != k){
ans.push_back(k);
return true;
}
}
ans.pop_back();
return false;
}
void solve(){
int n,m;
cin>>n>>m;
vector<int> adj[n+1];
while(m--){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<bool> vis(n+1,false);
vector<int> ans;
bool f = false;
for(int i=1;i<=n;i++){
if(!vis[i]){
if(dfs(adj,vis,i,ans,-1)){
int size = ans.size();
vector<int> tmp;
tmp.push_back(ans[size-1]);
for(int i=size-2;i>=0;i--){
if(ans[size-1]==ans[i]){
tmp.push_back(ans[i]);
break;
}
else
tmp.push_back(ans[i]);
}
f =true;
cout<<tmp.size()<<endl;
for(int i=0;i<tmp.size();i++){
cout<<tmp[i]<<" ";
}
cout<<endl;
break;
}
}
}
if(!f)
cout<<"IMPOSSIBLE\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//int t;
//cin>>t;
//while(t--)
solve();
return 0;
}