#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int const maxn = 202020;
vector<int> v[maxn];
ll res[maxn];
int solve(int x)
{
int sz = 1;
for (int u : v[x]) {
sz += solve(u);
res[x] += res[u];
}
res[x] += sz;
return sz;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(nullptr);
int n;
cin >> n;
for (int i = 2; i <= n; i++) {
int a;
cin >> a;
v[a].push_back(i);
}
solve(1);
for (int i = 1; i <= n; i++) cout << res[i] << " ";
cout << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5248 KB |
Output is correct |
2 |
Correct |
6 ms |
5248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5248 KB |
Output is correct |
2 |
Correct |
6 ms |
5288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5424 KB |
Output is correct |
2 |
Correct |
6 ms |
5424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5944 KB |
Output is correct |
2 |
Correct |
14 ms |
6412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
8004 KB |
Output is correct |
2 |
Correct |
27 ms |
8544 KB |
Output is correct |
3 |
Correct |
27 ms |
9984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
13124 KB |
Output is correct |
2 |
Correct |
66 ms |
16796 KB |
Output is correct |
3 |
Correct |
73 ms |
28300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
28300 KB |
Output is correct |
2 |
Correct |
69 ms |
28300 KB |
Output is correct |
3 |
Correct |
68 ms |
28300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
28300 KB |
Output is correct |
2 |
Correct |
66 ms |
28300 KB |
Output is correct |
3 |
Correct |
74 ms |
28300 KB |
Output is correct |