-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoundTrip.cpp
More file actions
68 lines (59 loc) · 1.07 KB
/
Copy pathRoundTrip.cpp
File metadata and controls
68 lines (59 loc) · 1.07 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
58
59
60
61
62
63
64
65
66
67
68
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
int n, m;
const int Sz = 1e5;
vector <int> a[Sz + 1];
void Read()
{
cin >> n >> m;
int u, v;
for(int i = 1; i <= m; i++)
{
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
}
int vis[Sz + 1];
int Pre[Sz + 1];
void DFS(int u, int p)
{
vis[u] = 1;
for(int v : a[u])
{
if(v == p) continue;
if(vis[v])
{
vector <int> res;
res.push_back(v);
res.push_back(u);
while(u != v)
{
u = Pre[u];
res.push_back(u);
}
cout << res.size() << "\n";
for(auto i : res) cout << i << ' ';
exit(0);
}
Pre[v] = u;
DFS(v, u);
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
Read();
for(int i = 1; i <= n; i++)
{
if(!vis[i])
{
DFS(i, 0);
}
}
cout << "IMPOSSIBLE";
}