-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1669 Round Trip.cpp
90 lines (64 loc) · 1.6 KB
/
1669 Round Trip.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
// JAI BAJARANG BALI
// manitianajay45
// give me some sunshine, give me some rain, give me another chance to grow up once again....
// sab moh maya hai....
// problem is asking about finding cycle with length>=3
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n,m;
vector<ll> graph[100005];
vector<bool> visited(100005,false);
vector<ll> parent(100005,-1);
vector<ll> vec;
void dfs(ll u,ll pr){
visited[u]=true;
parent[u]=pr;
if(vec.size()>=4){
return ;
}
for(auto i: graph[u]){
if(visited[i] && vec.size()<=3){
ll nd=u;
vec.clear();
vec.push_back(i);
while(nd!=i)
{
vec.push_back(nd);
nd=parent[nd];
}
vec.push_back(i);
reverse(vec.begin(),vec.end());
}else{
dfs(i,u);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
for(ll i=0;i<m;i++){
ll u,v;
cin>>u>>v;
graph[u].push_back(v);
graph[v].push_back(u);
}
for(ll i=1;i<=n;i++){
if(!visited[i]){
dfs(i,-1);
// cout<<"NO"<<endl;
if(vec.size()>=4){
cout<<vec.size()<<endl;
for(auto j:vec){
cout<<j<<" ";
}
cout<<endl;
return 0;
}
}
}
cout<<"IMPOSSIBLE"<<endl;
return 0;
}