Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int max_=500000;
- int tree_size[max_];
- vector<int> neig[max_];
- int r(int node){
- tree_size[node] = 1;
- for(int i=0; i<neig[node].size(); i++){
- r(neig[node][i]);
- tree_size[node] += tree_size[neig[node][i]];
- }
- return tree_size[node]+1;
- }
- int main()
- {
- int n;
- cin>>n;
- for(int i=2; i<=n; i++){
- int clen;
- cin>>clen;
- neig[clen].push_back(i);
- }
- r(1);
- for(int i=1; i<=n; i++){
- cout<<tree_size[i] - 1<<" ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement