#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 ll long long
#define fri(a) freopen(a,"r",stdin);
#define fro(a) freopen(a,"w",stdout);
const int N=1e5+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;
}
int 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 |
4 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2676 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2728 KB |
Output is correct |
2 |
Correct |
4 ms |
2812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
3192 KB |
Output is correct |
2 |
Correct |
11 ms |
3576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
4600 KB |
Output is correct |
2 |
Correct |
26 ms |
4856 KB |
Output is correct |
3 |
Correct |
26 ms |
5880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
38 ms |
5240 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
5168 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
38 ms |
5292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |