-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathround_trip.cpp
More file actions
53 lines (49 loc) · 1.05 KB
/
round_trip.cpp
File metadata and controls
53 lines (49 loc) · 1.05 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
#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<int> graph[100000];
bool vis[100000];
bool term;
deque<int> path;
void dfs(int id, int par){
path.push_back(id);
if(vis[id]){
while(true){
if(path.front() == id){
break;
}
path.pop_front();
}
term = true;
return;
}
vis[id] = true;
for(int i : graph[id]){
if(i == par) continue;
dfs(i, id);
if(term) return;
}
path.pop_back();
}
int32_t main(){
cin >> N >> M;
for(int i = 0; i < M; i++){
int a, b; cin >> a >> b;
graph[a-1].push_back(b-1);
graph[b-1].push_back(a-1);
}
for(int i = 0; i < N; i++){
if(vis[i]) continue;
dfs(i, i);
if(term){
cout << path.size() << endl;
while(!path.empty()){
cout << path.front() + 1 << " ";
path.pop_front();
}
cout << endl;
return 0;
}
}
cout << "IMPOSSIBLE" << endl;
}