#include <bits/stdc++.h>
using namespace std;
int 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 |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
376 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 |
1388 KB |
Output is correct |
2 |
Correct |
8 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
3948 KB |
Output is correct |
2 |
Correct |
26 ms |
4588 KB |
Output is correct |
3 |
Correct |
24 ms |
6252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
10604 KB |
Output is correct |
2 |
Incorrect |
66 ms |
14316 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
10604 KB |
Output is correct |
2 |
Correct |
69 ms |
12524 KB |
Output is correct |
3 |
Incorrect |
66 ms |
16236 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
10476 KB |
Output is correct |
2 |
Correct |
81 ms |
13088 KB |
Output is correct |
3 |
Incorrect |
66 ms |
19820 KB |
Output isn't correct |