fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. const int MAXN=2E5+1;
  6.  
  7. vector<ll>g[MAXN];
  8. vector<ll>subtree_size(MAXN,0);
  9.  
  10. void dfs(int node,int par){
  11. subtree_size[node]=1;
  12.  
  13. for(auto &c:g[node]){
  14. if(c != par){
  15. dfs(c,node);
  16. subtree_size[node] += subtree_size[c];
  17. }
  18. }
  19. }
  20.  
  21. void _144()
  22. {
  23.  
  24. ll n;
  25. cin>>n;
  26.  
  27. for(int i=2;i<=n;i++){
  28. ll x;
  29. cin>>x;
  30.  
  31. g[i].push_back(x);
  32. g[x].push_back(i);
  33. }
  34.  
  35. dfs(1,-1);
  36.  
  37. for(int i=1;i<=n;i++) cout<<subtree_size[i]-1<<" ";
  38. return;
  39. }
  40.  
  41. int main() {
  42. ios::sync_with_stdio(false);
  43. cin.tie(nullptr);
  44.  
  45. int t=1;
  46. // cin >> t;
  47.  
  48. while (t--) _144();
  49. return 0;
  50. }
Runtime error #stdin #stdout 0.05s 17212KB
stdin
Standard input is empty
stdout
Standard output is empty