Advertisement
Josif_tepe

Untitled

Oct 12th, 2023
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 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. if(neig[node].size()==0){
  9.     return 1;
  10. }
  11. else{
  12.     for(int i=0; i<neig[node].size(); i++){
  13.         tree_size[node]+=r(neig[node][i]);
  14.     }
  15. }
  16. return tree_size[node]+1;
  17. }
  18. int main()
  19. {
  20.     int n;
  21.     cin>>n;
  22.     for(int i=2; i<=n; i++){
  23.         int clen;
  24.         cin>>clen;
  25.         neig[clen].push_back(i);
  26.     }
  27.     r(1);
  28.     for(int i=1; i<=n; i++){
  29.         cout<<tree_size[i]<<" ";
  30.     }
  31.     return 0;
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement