Skip to content

Commit efd1fa0

Browse files
Create Round Trip 2.cpp
1 parent 663678d commit efd1fa0

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

Diff for: Round Trip 2.cpp

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
#define fi(a,b) for(int i=a;i<b;i++)
5+
#define fj(a,b) for(int j=a;j<b;j++)
6+
#define ff first
7+
#define ss second
8+
#define ll long long
9+
#define ld long double
10+
#define ull unsigned long long
11+
#define bp(x) __builtin_popcount(x)
12+
#define pr(x) for(auto it: x) cout<<it<<" "; cout<<endl;
13+
#define getMax(x) max_element(x.begin(),x.end())
14+
#define getMin(x) min_element(x.begin(),x.end())
15+
#define endl "\n"
16+
typedef vector<int> vi;
17+
typedef vector< pair<int,int> > vii;
18+
typedef vector<long long> vl;
19+
typedef pair<int,int> pii;
20+
typedef pair<ll,ll> pll;
21+
typedef vector< pair<ll,ll> > vll;
22+
//int dx[]={1,0,-1,0};
23+
//int dy[]={0,1,0,-1};
24+
//int dx[]={-1,0,1,1,1,0,-1,-1};
25+
//int dy[]={-1,-1,-1,0,1,1,1,0};
26+
27+
const int N = 1e5+10;
28+
vi adj[N];
29+
vi parent(N);
30+
vi color(N);
31+
32+
int start_node=-1, end_node=-1;
33+
34+
bool dfs(int node,int pre){
35+
color[node] = 1;
36+
for(auto it: adj[node]){
37+
// if(it==pre) continue;
38+
if(color[it]==0){
39+
parent[it] = node;
40+
if(dfs(it, node)) return 1;
41+
}
42+
else if(color[it]==1){
43+
start_node = it;
44+
end_node = node;
45+
return 1;
46+
}
47+
}
48+
color[node] = 2;
49+
return 0;
50+
}
51+
52+
void nikal_pehli_fursat_mai(){
53+
int n, m;
54+
cin>>n>>m;
55+
fi(0, m){
56+
int u, v;
57+
cin>>u>>v;
58+
adj[u].push_back(v);
59+
}
60+
vi ans;
61+
62+
fi(1, n+1){
63+
if(color[i]==0 and dfs(i, 0) and start_node!=-1){
64+
ans.push_back(start_node);
65+
for(int v = end_node; v != start_node; v = parent[v] ){
66+
ans.push_back(v);
67+
}
68+
ans.push_back(start_node);
69+
reverse(ans.begin(), ans.end());
70+
cout<<ans.size()<<endl;
71+
pr(ans);
72+
return;
73+
}
74+
}
75+
cout<<"IMPOSSIBLE"<<endl;
76+
}
77+
78+
int main(){
79+
#ifndef ONLINE_JUDGE
80+
freopen("input.txt", "r", stdin);
81+
freopen("output.txt", "w", stdout);
82+
#endif
83+
ios::sync_with_stdio(0);
84+
cin.tie(0);
85+
int tc=1;
86+
// cin>>tc;
87+
while(tc--){
88+
nikal_pehli_fursat_mai();
89+
}
90+
}

0 commit comments

Comments
 (0)