#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define f1 first
#define s2 second
#define pb push_back
#define mp make_pair
#define int long long
#define fri(a) freopen(a,"r",stdin);
#define fro(a) freopen(a,"w",stdout);
const int N=2e5+5;
int n,p[N],dp[N];
vector<int>v[N];
pair<int,int> dfs(int x){
int ans=0;
int child=0;
for(auto i:v[x]){
if(i!=p[i]){
pair<int,int>k=dfs(i);
ans+=k.f1;
child+=k.s2;
}
}
//cout<<x<<" "<<ans<<" "<<child<<endl;
dp[x]=ans+child+1;
pair<int,int>sa={ans+child+1,child+1};
return sa;
}
int32_t main(){
//fri("in.txt");
//fro("out.txt");
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
p[1]=1;
for(int i=2;i<=n;i++){
cin>>p[i];
v[p[i]].pb(i);
}
dfs(1);
for(int i=1;i<=n;i++){
cout<<dp[i]<<" ";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5752 KB |
Output is correct |
2 |
Correct |
13 ms |
6144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
7672 KB |
Output is correct |
2 |
Correct |
28 ms |
7900 KB |
Output is correct |
3 |
Correct |
28 ms |
8824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
12300 KB |
Output is correct |
2 |
Correct |
70 ms |
14468 KB |
Output is correct |
3 |
Correct |
75 ms |
25780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
12488 KB |
Output is correct |
2 |
Correct |
72 ms |
13480 KB |
Output is correct |
3 |
Correct |
69 ms |
15480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
12412 KB |
Output is correct |
2 |
Correct |
70 ms |
13688 KB |
Output is correct |
3 |
Correct |
69 ms |
17272 KB |
Output is correct |