#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> adj(n);
for (int i = 0; i < n - 1; i++) {
int v;
cin >> v;
adj[v - 1].push_back(i + 1);
}
vector<int> res(n);
function<pair<int, int>(int)> dfs = [&](int source) {
int sum = 0, children = 0;
for (int to : adj[source]) {
auto [s, c] = dfs(to);
sum += s;
children += c;
}
sum += children;
++sum;
res[source] = sum;
return make_pair(sum, 1 + children);
};
dfs(0);
for (int i = 0; i < n; i++) {
cout << res[i] << " \n"[i == n - 1];
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1516 KB |
Output is correct |
2 |
Correct |
8 ms |
2028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
4204 KB |
Output is correct |
2 |
Correct |
26 ms |
4460 KB |
Output is correct |
3 |
Correct |
24 ms |
6252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
10844 KB |
Output is correct |
2 |
Correct |
64 ms |
14188 KB |
Output is correct |
3 |
Correct |
76 ms |
34872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
10860 KB |
Output is correct |
2 |
Correct |
76 ms |
12524 KB |
Output is correct |
3 |
Correct |
68 ms |
16108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
10732 KB |
Output is correct |
2 |
Correct |
64 ms |
13052 KB |
Output is correct |
3 |
Correct |
68 ms |
19564 KB |
Output is correct |