-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintcycledfs.cpp
62 lines (56 loc) · 1.06 KB
/
printcycledfs.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;
#define mod 1000000007
#define ll long long int
#define mk make_pair
#define pb push_back
#define xx first
#define yy second
double pi=3.141592653589793238;
const int M = 1e9+7;
const int Nmax=5005;
const int MM = 1e7+1;
const int N=200010;
vector<int> adj[N];
bool vis[N];
int p[N];
void dfs(int x ,int pa){
p[x]=pa;
vis[x]=1;
for(auto i:adj[x]){
if(i==pa)continue;
else if(!vis[i])dfs(i,x);
else{
int k=x;
vector<int> ans;
while(k!=i){
ans.pb(k);
k=p[k];
}
ans.pb(i);
ans.pb(x);
cout<<ans.size()<<"\n";
for(auto j:ans)cout<<j+1<<" ";
exit(0);
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T=1;
//cin>>T;
while(T--){
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;x--;y--;
adj[x].pb(y);
adj[y].pb(x);
}
for(int i=0;i<n;i++){
if(!vis[i])dfs(i,-1);
}
cout<<"IMPOSSIBLE\n";
}
return