#include <bits/stdc++.h>
using namespace std;
long long f[200001];
int sz[200001];
vector<int> v[200001];
void dfs(int cur,int prev)
{
f[cur]=1;
sz[cur]=1;
for(auto node : v[cur])
{
if(node==prev)continue;
dfs(node,cur);
f[cur]+=f[node]+sz[node];
sz[cur]+=sz[node];
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,x;
cin >> n;
for(int i=1;i<n;i++)
{
cin >> x;
x--;
v[i].push_back(x);
v[x].push_back(i);
}
dfs(0,-1);
for(int i=0;i<n;i++)
{
cout << f[i]<<' ';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
6124 KB |
Output is correct |
2 |
Correct |
14 ms |
6508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
8684 KB |
Output is correct |
2 |
Correct |
30 ms |
8848 KB |
Output is correct |
3 |
Correct |
28 ms |
10220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
132 ms |
15600 KB |
Output is correct |
2 |
Correct |
73 ms |
17772 KB |
Output is correct |
3 |
Correct |
77 ms |
32492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
131 ms |
15544 KB |
Output is correct |
2 |
Correct |
80 ms |
16364 KB |
Output is correct |
3 |
Correct |
72 ms |
19308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
119 ms |
15240 KB |
Output is correct |
2 |
Correct |
75 ms |
16748 KB |
Output is correct |
3 |
Correct |
76 ms |
21996 KB |
Output is correct |