#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+5;
ll ans[N], sz[N];
vector<int> child[N];
void dfs(int v)
{
sz[v] = 1;
for(int u : child[v])
{
dfs(u);
ans[v] += ans[u];
sz[v] += sz[u];
}
ans[v] += sz[v];
}
int main()
{
int n;
cin >> n;
for(int i = 2; i <= n; i ++)
{
int x;
cin >> x;
child[x].push_back(i);
}
dfs(1);
for(int i = 1; i <= n; i++)
cout << ans[i] << ' ';
cout << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
7260 KB |
Output is correct |
2 |
Correct |
13 ms |
7720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
8796 KB |
Output is correct |
2 |
Correct |
21 ms |
9296 KB |
Output is correct |
3 |
Correct |
20 ms |
10472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
12884 KB |
Output is correct |
2 |
Correct |
61 ms |
15700 KB |
Output is correct |
3 |
Correct |
65 ms |
30116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
12852 KB |
Output is correct |
2 |
Correct |
63 ms |
14624 KB |
Output is correct |
3 |
Correct |
64 ms |
17156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
12884 KB |
Output is correct |
2 |
Correct |
62 ms |
14932 KB |
Output is correct |
3 |
Correct |
64 ms |
19540 KB |
Output is correct |