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){
- if(neig[node].size()==0){
- return 1;
- }
- else{
- for(int i=0; i<neig[node].size(); i++){
- tree_size[node]+=r(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]<<" ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement