-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1669.cpp
52 lines (49 loc) · 1011 Bytes
/
1669.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
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, p[N], vstart = -1, vend = -1;
vector<int> adj[N];
bool dfs(int u) {
for (int v: adj[u]) {
if (v == p[u]) continue;
if (p[v]) {
vstart = v;
vend = u;
return true;
} else {
p[v] = u;
if (dfs(v)) return true;
}
}
return false;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1, u, v; i <= m; ++i) {
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
for (int i = 1; i <= n; ++i) {
if (p[i]) continue;
p[i] = -1;
if (dfs(i)) break;
}
if (vstart == -1) {
cout << "IMPOSSIBLE\n";
} else {
vector<int> vert;
for (int u = vend; u != vstart; u = p[u]) {
vert.emplace_back(u);
}
vert.emplace_back(vstart);
vert.emplace_back(vend);
cout << vert.size() << "\n";
for (int u: vert) {
cout << u << " ";
}
cout << "\n";
}
return 0;
}