-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRound Trip II.cpp
67 lines (54 loc) · 1.19 KB
/
Round Trip II.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 pii pair<int,int>
#define F first
#define S second
#define ll long long
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
const int N = 100005;
vector<int> adj[N];
int color[N];
int parent[N];
void dfs(int u){
color[u] = 1;
for(auto v:adj[u]){
if( color[v] == 0 ) {
parent[v] = u;
dfs(v);
}else if( color[v] == 1 ){
int curr = u;
vector<int> path;
path.push_back(v);
while(curr!=v){
path.push_back(curr);
curr = parent[curr];
}
path.push_back(v);
cout<<path.size()<<endl;
for(int i=path.size()-1;i>=0;i--) cout<<path[i]<<" ";
exit(0);
}
}
color[u] = 2;
}
void solve(){
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
}
for(int i=1;i<=n;i++){
if( color[i] == 0 ) dfs(i);
}
cout<<"IMPOSSIBLE";
}
int main(){
// int tc;
// cin>>tc;
// for(int t=1;t<=tc;t++){
// cout<<"Case "<<t<<": ";
solve();
// }
}