diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2026-07-06 19:08:08 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2026-07-06 19:08:08 +0200 |
| commit | 96254947699986c59f0dc63d69fd4b76bd3ed43e (patch) | |
| tree | 6c4dca945d7f7427c48be234d827fe4d33be02c5 /06_tree_algorithms | |
| download | cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.tar.gz cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.zip | |
Initial commit
Diffstat (limited to '06_tree_algorithms')
| -rwxr-xr-x | 06_tree_algorithms/a.out | bin | 0 -> 34024 bytes | |||
| -rw-r--r-- | 06_tree_algorithms/subordinates_1674.cpp | 23 | ||||
| -rw-r--r-- | 06_tree_algorithms/tree_matching_1130.cpp | 41 |
3 files changed, 64 insertions, 0 deletions
diff --git a/06_tree_algorithms/a.out b/06_tree_algorithms/a.out new file mode 100755 index 0000000..ec817fa --- /dev/null +++ b/06_tree_algorithms/a.out | |||
| Binary files differ | |||
diff --git a/06_tree_algorithms/subordinates_1674.cpp b/06_tree_algorithms/subordinates_1674.cpp new file mode 100644 index 0000000..38982ce --- /dev/null +++ b/06_tree_algorithms/subordinates_1674.cpp | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | #include <iostream> | ||
| 2 | #include <vector> | ||
| 3 | |||
| 4 | int f(const std::vector<std::vector<int>>& a, std::vector<int>& b, int i) { | ||
| 5 | for (auto x : a[i]) | ||
| 6 | b[i] += f(a, b, x) + 1; | ||
| 7 | return b[i]; | ||
| 8 | } | ||
| 9 | |||
| 10 | int main() { | ||
| 11 | int n, x; | ||
| 12 | std::cin >> n; | ||
| 13 | std::vector<std::vector<int>> a(n); | ||
| 14 | for (int i = 1; i < n; i++) { | ||
| 15 | std::cin >> x; | ||
| 16 | a[x-1].push_back(i); | ||
| 17 | } | ||
| 18 | std::vector<int> b(n, 0); | ||
| 19 | f(a, b, 0); | ||
| 20 | for (auto y : b) | ||
| 21 | std::cout << y << " "; | ||
| 22 | std::cout << std::endl; | ||
| 23 | } | ||
diff --git a/06_tree_algorithms/tree_matching_1130.cpp b/06_tree_algorithms/tree_matching_1130.cpp new file mode 100644 index 0000000..8828f68 --- /dev/null +++ b/06_tree_algorithms/tree_matching_1130.cpp | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | #include <iostream> | ||
| 2 | #include <vector> | ||
| 3 | |||
| 4 | int f(const std::vector<std::vector<int>>& a, | ||
| 5 | std::vector<std::vector<int>>& t, int v, int p, bool x) { | ||
| 6 | if (t[v][x] != -1) return t[v][x]; | ||
| 7 | if (p != -1 && a[v].size() == 1) return t[v][x] = 0; | ||
| 8 | |||
| 9 | t[v][x] = 0; | ||
| 10 | for (auto u : a[v]) { | ||
| 11 | if (u == p) continue; | ||
| 12 | t[v][x] += f(a, t, u, v, false); | ||
| 13 | } | ||
| 14 | if (x) return t[v][x]; | ||
| 15 | |||
| 16 | for (auto u : a[v]) { | ||
| 17 | if (u == p) continue; | ||
| 18 | if (f(a, t, u, v, true) == t[u][false]) { | ||
| 19 | t[v][x]++; | ||
| 20 | break; | ||
| 21 | } | ||
| 22 | } | ||
| 23 | return t[v][x]; | ||
| 24 | } | ||
| 25 | |||
| 26 | int main() { | ||
| 27 | int n, x, y; | ||
| 28 | std::cin >> n; | ||
| 29 | std::vector<std::vector<int>> a(n); | ||
| 30 | for (int i = 0; i < n-1; i++) { | ||
| 31 | std::cin >> x >> y; | ||
| 32 | a[x-1].push_back(y-1); | ||
| 33 | a[y-1].push_back(x-1); | ||
| 34 | } | ||
| 35 | if (n == 1) { | ||
| 36 | std::cout << "0\n"; | ||
| 37 | return 0; | ||
| 38 | } | ||
| 39 | std::vector<std::vector<int>> t(n, {-1, -1}); | ||
| 40 | std::cout << f(a, t, 0, -1, false) << "\n"; | ||
| 41 | } | ||
