Advertisement
Josif_tepe

Untitled

Oct 12th, 2023
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. const int max_=500000;
  5. int tree_size[max_];
  6. vector<int> neig[max_];
  7. int r(int node){
  8.     tree_size[node] = 1;
  9.  
  10.     for(int i=0; i<neig[node].size(); i++){
  11.         r(neig[node][i]);
  12.         tree_size[node] += tree_size[neig[node][i]];
  13.        
  14.     }
  15. return tree_size[node]+1;
  16. }
  17. int main()
  18. {
  19.     int n;
  20.     cin>>n;
  21.     for(int i=2; i<=n; i++){
  22.         int clen;
  23.         cin>>clen;
  24.         neig[clen].push_back(i);
  25.     }
  26.     r(1);
  27.     for(int i=1; i<=n; i++){
  28.         cout<<tree_size[i] - 1<<" ";
  29.     }
  30.     return 0;
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement