aboutsummaryrefslogtreecommitdiff
path: root/04_graph_algorithms
diff options
context:
space:
mode:
Diffstat (limited to '04_graph_algorithms')
-rwxr-xr-x04_graph_algorithms/a.outbin0 -> 14176 bytes
-rw-r--r--04_graph_algorithms/building_roads_1666.cpp32
-rw-r--r--04_graph_algorithms/building_teams_1668.cpp37
-rw-r--r--04_graph_algorithms/counting_rooms_1192.cpp62
-rw-r--r--04_graph_algorithms/high_score_1673.cpp61
-rw-r--r--04_graph_algorithms/labyrinth_1193.cpp131
-rw-r--r--04_graph_algorithms/message_route_1667.cpp54
-rw-r--r--04_graph_algorithms/round_trip_1669.cpp44
-rw-r--r--04_graph_algorithms/shortest_routes_i_1671.cpp43
-rw-r--r--04_graph_algorithms/shortest_routes_ii_1672.cpp30
10 files changed, 494 insertions, 0 deletions
diff --git a/04_graph_algorithms/a.out b/04_graph_algorithms/a.out
new file mode 100755
index 0000000..ae515d4
--- /dev/null
+++ b/04_graph_algorithms/a.out
Binary files differ
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}
diff --git a/04_graph_algorithms/building_teams_1668.cpp b/04_graph_algorithms/building_teams_1668.cpp
new file mode 100644
index 0000000..c98e14d
--- /dev/null
+++ b/04_graph_algorithms/building_teams_1668.cpp
@@ -0,0 +1,37 @@
1#include <iostream>
2#include <vector>
3
4bool visit(size_t i, int c, const std::vector<std::vector<size_t>>& a,
5 std::vector<int>& t) {
6 if (t[i] != 0) return true;
7 t[i] = c;
8 for (auto j : a[i])
9 if (t[j] == c)
10 return false;
11 else if (!visit(j, 3-c, a, t)) return false;
12 return true;
13}
14
15int main() {
16 size_t n, m;
17 std::cin >> n >> m;
18 std::vector<std::vector<size_t>> a(n);
19 for (size_t i = 0; i < m; i++) {
20 size_t x, y;
21 std::cin >> x >> y;
22 a[x-1].push_back(y-1);
23 a[y-1].push_back(x-1);
24 }
25
26 std::vector<int> t(n);
27 for (size_t i = 0; i < n; i++) {
28 if (t[i] == 0) {
29 if (!visit(i, 1, a, t)) {
30 std::cout << "IMPOSSIBLE\n";
31 return 0;
32 }
33 }
34 }
35 for (auto x : t) std::cout << x << " ";
36 std::cout << "\n";
37}
diff --git a/04_graph_algorithms/counting_rooms_1192.cpp b/04_graph_algorithms/counting_rooms_1192.cpp
new file mode 100644
index 0000000..dc71529
--- /dev/null
+++ b/04_graph_algorithms/counting_rooms_1192.cpp
@@ -0,0 +1,62 @@
1#include <iostream>
2#include <string>
3#include <vector>
4
5class Tile {
6public:
7 int i;
8 int j;
9 std::vector<Tile> neighbors() const {
10 return {Tile{i-1,j}, Tile{i+1,j}, Tile{i,j-1}, Tile{i,j+1}};
11 }
12};
13
14class Map {
15public:
16 int n;
17 int m;
18
19 Map(int i, int j) : n{i}, m{j}, c(n*m, 0) {}
20 int color(Tile t) const { return inbound(t) ? c[ind(t)] : -1; }
21 void setcolor(Tile t, int k) { if (inbound(t)) c[ind(t)] = k; }
22 bool wall(Tile t) const { return !inbound(t) || c[ind(t)] == -1; }
23 void setwall(Tile t) { if (inbound(t)) setcolor(t, -1); }
24private:
25 std::vector<int> c;
26
27 size_t ind(Tile t) const { return m*t.i + t.j; }
28 bool inbound(Tile t) const {
29 return t.i >= 0 && t.i < n && t.j >= 0 && t.j < m;
30 }
31};
32
33Map readmap() {
34 int n, m;
35 std::string s;
36 std::cin >> n >> m;
37 Map map(n, m);
38 for (int i = 0; i < n; i++) {
39 std::cin >> s;
40 for (int j = 0; j < m; j++)
41 if (s[j] == '#')
42 map.setwall(Tile{i, j});
43 }
44 return map;
45}
46
47void visit(Map& m, Tile t, int c) {
48 m.setcolor(t, c);
49 for (auto u : t.neighbors())
50 if (m.color(u) == 0)
51 visit(m, u, c);
52}
53
54int main() {
55 auto m = readmap();
56 int c{0};
57 for (int i = 0; i < m.n; i++)
58 for (int j = 0; j < m.m; j++)
59 if (m.color(Tile{i, j}) == 0)
60 visit(m, Tile{i, j}, ++c);
61 std::cout << c << "\n";
62}
diff --git a/04_graph_algorithms/high_score_1673.cpp b/04_graph_algorithms/high_score_1673.cpp
new file mode 100644
index 0000000..c327ba0
--- /dev/null
+++ b/04_graph_algorithms/high_score_1673.cpp
@@ -0,0 +1,61 @@
1#include <algorithm>
2#include <iostream>
3#include <tuple>
4#include <vector>
5
6constexpr long long inf = 999999999999999LL;
7
8size_t findcycle(
9 size_t v,
10 const std::vector<std::tuple<size_t, size_t, long long>>& a,
11 const std::vector<size_t>& p
12) {
13 std::vector<bool> visited(p.size(), false);
14 visited[v] = true;
15 size_t u = p[v];
16 while (!visited[u]) {
17 visited[u] = true;
18 u = p[u];
19 }
20 return u;
21}
22
23int main() {
24 size_t n, m;
25 std::cin >> n >> m;
26 std::vector<std::tuple<size_t, size_t, long long>> a(m);
27 for (size_t i = 0; i < m; i++) {
28 size_t x, y;
29 long long w;
30 std::cin >> x >> y >> w;
31 a[i] = {x-1, y-1, w};
32 }
33
34 std::vector<size_t> p(n, n);
35 std::vector<long long> d(n, -inf);
36 d[0] = 0;
37 std::vector<bool> reach_1(n, false), reach_n(n, false);
38 reach_1[0] = reach_n[n-1] = true;
39 for (size_t i = 0; i < n; i++) {
40 for (auto [v, u, w] : a) {
41 if (d[u] < d[v] + w) {
42 d[u] = d[v] + w;
43 p[u] = v;
44 }
45 reach_n[v] = reach_n[v] || reach_n[u];
46 reach_1[u] = reach_1[u] || reach_1[v];
47 }
48 }
49
50 for (auto [v, u, w] : a) {
51 if (d[u] < d[v] + w) {
52 size_t x = findcycle(u, a, p);
53 if (reach_1[x] && reach_n[x]) {
54 std::cout << "-1\n";
55 return 0;
56 }
57 }
58 }
59
60 std::cout << d[n-1] << "\n";
61}
diff --git a/04_graph_algorithms/labyrinth_1193.cpp b/04_graph_algorithms/labyrinth_1193.cpp
new file mode 100644
index 0000000..30f8c0b
--- /dev/null
+++ b/04_graph_algorithms/labyrinth_1193.cpp
@@ -0,0 +1,131 @@
1#include <iostream>
2#include <queue>
3#include <ranges>
4#include <string>
5#include <tuple>
6#include <vector>
7
8struct Pos {
9 size_t i;
10 size_t j;
11
12 Pos(size_t a = 0, size_t b = 0) : i{a}, j{b} {}
13 bool operator==(const Pos& p) const { return i == p.i && j == p.j; }
14
15 std::vector<Pos> neighbors() const {
16 return {Pos(i+1, j), Pos(i-1, j), Pos(i, j+1), Pos(i, j-1)};
17 }
18
19 char dir(const Pos& p) const {
20 if (p.i == i+1 && p.j == j) return 'D';
21 if (p.i == i-1 && p.j == j) return 'U';
22 if (p.i == i && p.j == j+1) return 'R';
23 if (p.i == i && p.j == j-1) return 'L';
24 return 'X';
25 }
26};
27
28class Map {
29public:
30 Map(size_t i, size_t j) : n{i}, m{j}, v(n, std::vector<bool>(m)) {}
31 Pos start() const { return a; }
32 Pos finish() const { return b; }
33 bool operator[](Pos p) const { return inb(p) && v[p.i][p.j]; }
34
35 friend std::istream& operator>>(std::istream& is, Map& map) {
36 std::string s;
37 is >> s;
38 for (size_t j = 0; j < map.m; j++)
39 map.readchar(s[j], map.l, j);
40 map.l++;
41 return is;
42 }
43
44 friend std::ostream& operator<<(std::ostream& os, const Map& map) {
45 for (size_t i = 0; i < map.n; i++) {
46 for (size_t j = 0; j < map.m; j++) {
47 if (map.a == Pos(i, j)) os << 'A';
48 else if (map.b == Pos(i, j)) os << 'B';
49 else os << (map.v[i][j] ? '.' : '#');
50 }
51 os << "\n";
52 }
53 return os;
54 }
55
56 template<typename T>
57 using Overlay = std::pair<T, std::vector<std::vector<T>>>;
58
59 template<typename T>
60 Overlay<T> overlay(T t) const {
61 auto ov = std::vector<std::vector<T>>(n, std::vector(m, t));
62 return {t, ov};
63 }
64
65 template<typename T>
66 T at(const Overlay<T>& ov, Pos p) const {
67 return inb(p) ? ov.second[p.i][p.j] : ov.first;
68 }
69
70 template<typename T>
71 void set(Overlay<T>& ov, Pos p, T val) const {
72 if (inb(p)) ov.second[p.i][p.j] = val;
73 }
74private:
75 size_t l{0};
76 size_t n;
77 size_t m;
78 Pos a;
79 Pos b;
80 std::vector<std::vector<bool>> v;
81
82 void readchar(char c, size_t i, size_t j) {
83 v[i][j] = c != '#';
84 if (c == 'A') a = {i, j};
85 if (c == 'B') b = {i, j};
86 }
87
88 bool inb(Pos p) const { return p.i < n && p.j < m; }
89};
90
91int main() {
92 size_t n, m;
93 std::cin >> n >> m;
94 Map map(n, m);
95 for (size_t i = 0; i < n; i++)
96 std::cin >> map;
97
98 constexpr size_t inf{999999999};
99 auto d = map.overlay<size_t>(inf);
100 std::queue<std::pair<Pos, size_t>> q;
101 q.push({map.start(), 0});
102 while (!q.empty()) {
103 auto [p, w] = q.front();
104 q.pop();
105 if (!map[p] || map.at(d, p) != inf) continue;
106 map.set(d, p, w);
107 for (auto x : p.neighbors()) q.push({x, w+1});
108 }
109
110 if (map.at(d, map.finish()) == inf) {
111 std::cout << "NO\n";
112 } else {
113 std::cout << "YES\n" << map.at(d, map.finish()) << "\n";
114
115 // Backtrack
116 Pos p = map.finish();
117 std::vector<char> path;
118 do {
119 for (auto x : p.neighbors()) {
120 if (map.at(d, x) == map.at(d, p)-1) {
121 path.push_back(x.dir(p));
122 p = x;
123 break;
124 }
125 }
126 } while (p != map.start());
127 for (auto c : path | std::views::reverse)
128 std::cout << c;
129 std::cout << "\n";
130 }
131}
diff --git a/04_graph_algorithms/message_route_1667.cpp b/04_graph_algorithms/message_route_1667.cpp
new file mode 100644
index 0000000..280f7d6
--- /dev/null
+++ b/04_graph_algorithms/message_route_1667.cpp
@@ -0,0 +1,54 @@
1#include <iostream>
2#include <queue>
3#include <ranges>
4#include <vector>
5
6constexpr size_t inf = 1999999999;
7
8int main() {
9 size_t n, m;
10 std::cin >> n >> m;
11 std::vector<std::vector<size_t>> a(n);
12 for (size_t i = 0; i < m; i++) {
13 size_t x, y;
14 std::cin >> x >> y;
15 a[x-1].push_back(y-1);
16 a[y-1].push_back(x-1);
17 }
18
19 std::queue<size_t> q;
20 std::vector<size_t> d(n, inf);
21 d[0] = 0;
22 q.push(0);
23 while (!q.empty()) {
24 auto i = q.front();
25 q.pop();
26 if (i == n-1) break;
27 for (auto j : a[i]) {
28 if (d[j] > d[i]+1) {
29 d[j] = d[i] + 1;
30 q.push(j);
31 }
32 }
33 }
34
35 if (d[n-1] == inf) {
36 std::cout << "IMPOSSIBLE\n";
37 } else {
38 std::cout << d[n-1]+1 << "\n";
39 // Backtracking
40 std::vector<size_t> v;
41 v.push_back(n-1);
42 while (v.back() != 0) {
43 for (auto j : a[v.back()]) {
44 if (d[j] == d[v.back()]-1) {
45 v.push_back(j);
46 break;
47 }
48 }
49 }
50 for (auto x : v | std::views::reverse)
51 std::cout << x+1 << " ";
52 std::cout << "\n";
53 }
54}
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}
diff --git a/04_graph_algorithms/shortest_routes_i_1671.cpp b/04_graph_algorithms/shortest_routes_i_1671.cpp
new file mode 100644
index 0000000..89f675d
--- /dev/null
+++ b/04_graph_algorithms/shortest_routes_i_1671.cpp
@@ -0,0 +1,43 @@
1#include <iostream>
2#include <queue>
3#include <utility>
4#include <vector>
5
6class dvpair {
7public:
8 size_t d;
9 size_t v;
10 auto operator<=>(const dvpair& p) const { return p.d <=> d; }
11};
12
13int main() {
14 size_t n, m;
15 std::cin >> n >> m;
16 std::vector<std::vector<std::pair<size_t, size_t>>> a(n);
17 for (size_t i = 0; i < m; i++) {
18 size_t x, y, z;
19 std::cin >> x >> y >> z;
20 a[x-1].push_back({y-1, z});
21 }
22
23 constexpr size_t inf{999999999999999ULL};
24 std::vector<size_t> d(n, inf);
25 std::priority_queue<dvpair> q;
26 d[0] = 0;
27 q.push({0, 0});
28 while (!q.empty()) {
29 auto [dd, p] = q.top();
30 q.pop();
31 if (dd > d[p])
32 continue;
33 for (auto [r, w] : a[p]) {
34 if (w + d[p] < d[r]) {
35 d[r] = d[p] + w;
36 q.push({d[r], r});
37 }
38 }
39 }
40 for (auto x : d)
41 std::cout << x << " ";
42 std::cout << "\n";
43}
diff --git a/04_graph_algorithms/shortest_routes_ii_1672.cpp b/04_graph_algorithms/shortest_routes_ii_1672.cpp
new file mode 100644
index 0000000..d49cb71
--- /dev/null
+++ b/04_graph_algorithms/shortest_routes_ii_1672.cpp
@@ -0,0 +1,30 @@
1#include <algorithm>
2#include <iostream>
3#include <vector>
4
5constexpr size_t inf{999999999999999ULL};
6
7int main() {
8 size_t n, m, q;
9 std::cin >> n >> m >> q;
10 std::vector<std::vector<size_t>> d(n, std::vector(n, inf));
11 for (size_t i = 0; i < n; i++) d[i][i] = 0;
12 for (size_t i = 0; i < m; i++) {
13 size_t x, y, z;
14 std::cin >> x >> y >> z;
15 d[x-1][y-1] = d[y-1][x-1] = std::min(d[x-1][y-1], z);
16 }
17
18 for (size_t k = 0; k < n; k++)
19 for (size_t i = 0; i < n; i++)
20 for (size_t j = 0; j < n; j++)
21 d[j][i] = d[i][j] =
22 std::min(d[i][j], d[i][k] + d[k][j]);
23
24 for (size_t i = 0; i < q; i++) {
25 size_t x, y;
26 std::cin >> x >> y;
27 long long int dd = d[x-1][y-1];
28 std::cout << (dd == inf ? -1LL : dd) << "\n";
29 }
30}

Generated with cgit - Back to sebastiano.tronto.net