aboutsummaryrefslogtreecommitdiff
path: root/06_tree_algorithms/subordinates_1674.cpp
diff options
context:
space:
mode:
Diffstat (limited to '06_tree_algorithms/subordinates_1674.cpp')
-rw-r--r--06_tree_algorithms/subordinates_1674.cpp23
1 files changed, 23 insertions, 0 deletions
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
4int 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
10int 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}

Generated with cgit - Back to sebastiano.tronto.net