This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |