aboutsummaryrefslogtreecommitdiff
path: root/04_graph_algorithms/building_roads_1666.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/building_roads_1666.cpp
downloadcses-96254947699986c59f0dc63d69fd4b76bd3ed43e.tar.gz
cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.zip
Initial commit
Diffstat (limited to '04_graph_algorithms/building_roads_1666.cpp')
-rw-r--r--04_graph_algorithms/building_roads_1666.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/04_graph_algorithms/building_roads_1666.cpp b/04_graph_algorithms/building_roads_1666.cpp
new file mode 100644
index 0000000..cf6ac28
--- /dev/null
+++ b/04_graph_algorithms/building_roads_1666.cpp
@@ -0,0 +1,32 @@
1#include <iostream>
2#include <vector>
3
4void visit(size_t i, const std::vector<std::vector<size_t>>& a,
5 std::vector<bool>& c) {
6 if (c[i]) return;
7 c[i] = true;
8 for (auto j : a[i])
9 visit(j, a, c);
10}
11
12int main() {
13 size_t n, m;
14 std::cin >> n >> m;
15 std::vector<std::vector<size_t>> a(n);
16 for (size_t i = 0; i < m; i++) {
17 size_t x, y;
18 std::cin >> x >> y;
19 a[x-1].push_back(y-1);
20 a[y-1].push_back(x-1);
21 }
22
23 std::vector<bool> c(n, false);
24 std::vector<size_t> s;
25 for (size_t i = 0; i < n; i++) {
26 if (c[i]) continue;
27 if (i != 0) s.push_back(i);
28 visit(i, a, c);
29 }
30 std::cout << s.size() << "\n";
31 for (auto x : s) std::cout << "1 " << x+1 << "\n";
32}

Generated with cgit - Back to sebastiano.tronto.net