-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathround_trip_ii.cpp
More file actions
57 lines (53 loc) · 1.22 KB
/
round_trip_ii.cpp
File metadata and controls
57 lines (53 loc) · 1.22 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
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
int N, M;
vector<int> graph[100000], cycle;
bool vis1[100000], vis2[100000], fcycle;
void dfs(int id){
if(vis2[id]){
fcycle = true;
cycle.push_back(id);
return;
}
if(vis1[id]) return;
vis1[id] = true;
vis2[id] = true;
cycle.push_back(id);
for(int i : graph[id]){
dfs(i);
if(fcycle) return;
}
cycle.pop_back();
vis2[id] = false;
}
int32_t main(){
cin.tie(0) -> sync_with_stdio(0);
cin >> N >> M;
for(int i = 0; i < M; i++){
int a, b; cin >> a >> b;
graph[a-1].push_back(b-1);
}
for(int i = 0; i < N; i++){
dfs(i);
if(fcycle){
stack<int> ans;
int v = cycle.back();
cycle.pop_back();
ans.push(v);
while(cycle.back() != v){
ans.push(cycle.back());
cycle.pop_back();
}
ans.push(v);
cout << ans.size() << endl;
while(!ans.empty()){
cout << ans.top()+1 << " ";
ans.pop();
}
cout << endl;
return 0;
}
}
cout << "IMPOSSIBLE" << endl;
}