diff --git a/9_tree_algorithms/subordinates.cpp b/9_tree_algorithms/subordinates.cpp new file mode 100644 index 0000000..565b021 --- /dev/null +++ b/9_tree_algorithms/subordinates.cpp @@ -0,0 +1,45 @@ +#include +#include + +using namespace std; + +int n; +vector> adj; +vector subordinates; + +int dfs(int u) { + int current_subtree_size = 1; + + for (int v : adj[u]) { + current_subtree_size += dfs(v); + } + + subordinates[u] = current_subtree_size - 1; + + return current_subtree_size; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + cin >> n; + + adj.resize(n + 1); + subordinates.resize(n + 1); + + for (int i = 2; i <= n; ++i) { + int boss; + cin >> boss; + adj[boss].push_back(i); + } + + dfs(1); + + for (int i = 1; i <= n; ++i) { + cout << subordinates[i] << " "; + } + cout << "\n"; + + return 0; +} \ No newline at end of file