-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcses_1669.cpp
More file actions
61 lines (57 loc) · 1.06 KB
/
cses_1669.cpp
File metadata and controls
61 lines (57 loc) · 1.06 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
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 100087;
int n, m, a, b, repeat;
vector<int> G[MAXN], route;
bool visited[MAXN], flag;
bool dfs(int u, int pre)
{
if (visited[u])
{
route.emplace_back(u);
repeat = u;
return true;
}
visited[u] = true;
for (auto x : G[u])
{
if (x != pre && dfs(x, u))
{
if (!flag)
route.emplace_back(u);
if (repeat == u)
flag = true;
return true;
}
}
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
while (m--)
{
cin >> a >> b;
G[a].emplace_back(b);
G[b].emplace_back(a);
}
for (int i = 1; i <= n; i++)
{
if (!visited[i])
dfs(i, -87);
if (repeat)
break;
}
if (!repeat)
cout << "IMPOSSIBLE\n";
else
{
cout << route.size() << "\n";
for (auto x : route)
cout << x << " ";
}
return 0;
}