#include <bits/stdc++.h>
using namespace std;
#define int long long
const int M = 2e5 + 1;
int subt[M],ans[M];
vector<int> nei[M];
void dfs(int u)
{
subt[u]=1;
for (int i:nei[u])
{
dfs(i);
ans[u]+=ans[i];
subt[u]+=subt[i];
}
ans[u]+=subt[u];
}
signed main()
{
int n;
cin>>n;
for (int i=2;i<=n;i++)
{
int u;
cin>>u;
nei[u].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 |
1 ms |
6744 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 |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 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 |
6 ms |
7256 KB |
Output is correct |
2 |
Correct |
10 ms |
7508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
8536 KB |
Output is correct |
2 |
Correct |
19 ms |
8796 KB |
Output is correct |
3 |
Correct |
20 ms |
10076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
12368 KB |
Output is correct |
2 |
Correct |
55 ms |
14724 KB |
Output is correct |
3 |
Correct |
70 ms |
28788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
12220 KB |
Output is correct |
2 |
Correct |
68 ms |
13648 KB |
Output is correct |
3 |
Correct |
58 ms |
15952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
12112 KB |
Output is correct |
2 |
Correct |
61 ms |
13904 KB |
Output is correct |
3 |
Correct |
60 ms |
18420 KB |
Output is correct |