aboutsummaryrefslogtreecommitdiff
path: root/04_graph_algorithms/round_trip_1669.cpp
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
commit96254947699986c59f0dc63d69fd4b76bd3ed43e (patch)
tree6c4dca945d7f7427c48be234d827fe4d33be02c5 /04_graph_algorithms/round_trip_1669.cpp
downloadcses-96254947699986c59f0dc63d69fd4b76bd3ed43e.tar.gz
cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.zip
Initial commit
Diffstat (limited to '04_graph_algorithms/round_trip_1669.cpp')
-rw-r--r--04_graph_algorithms/round_trip_1669.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/04_graph_algorithms/round_trip_1669.cpp b/04_graph_algorithms/round_trip_1669.cpp
new file mode 100644
index 0000000..c19b29d
--- /dev/null
+++ b/04_graph_algorithms/round_trip_1669.cpp
@@ -0,0 +1,44 @@
1#include <iostream>
2#include <vector>
3
4std::vector<int> cyc;
5
6int dfs(const std::vector<std::vector<int>>& a,
7 std::vector<bool>& v, int i, int p) {
8 v[i] = true;
9 for (auto x : a.at(i)) {
10 if (x == p) continue;
11 if (v.at(x)) {
12 cyc.push_back(x);
13 cyc.push_back(i);
14 return x;
15 }
16 if (int r = dfs(a, v, x, i); r != 0) {
17 cyc.push_back(i);
18 if (r == i) {
19 std::cout << cyc.size() << "\n";
20 for (auto y : cyc) std::cout << y << " ";
21 std::cout << "\n";
22 exit(0);
23 } else return r;
24 }
25 }
26 return 0;
27}
28
29int main() {
30 int n, m;
31 std::cin >> n >> m;
32 std::vector<std::vector<int>> a(n+1);
33 for (int i = 0; i < m; i++) {
34 int x, y;
35 std::cin >> x >> y;
36 a[x].push_back(y);
37 a[y].push_back(x);
38 }
39 std::vector<bool> v(n+1);
40 for (int i = 1; i <= n; i++)
41 if (!v.at(i))
42 dfs(a, v, i, 0);
43 std::cout << "IMPOSSIBLE\n";
44}

Generated with cgit - Back to sebastiano.tronto.net