Help about Finding the centroid of a tree...What's the difference between these two methods?

Revision en3, by -IA-, 2017-09-05 14:00:16

The original problem is AIM TECH Round4 div.2 problem E.844E

I read the tutorial and read some book about methods of Finding centroids in a tree,and a friend told me that if a tree have two centroid,then the max size of the subtree of each of these centroids is equal to N/2(N is the number of vertexes in a tree).

I wrote this method to find centroid but I got WA on 11,but I wrote the second method to find that I got AC.What's the differences between them?Tell me please,thank you in advance:-)

the code I got WA:submission here,in my another account

pair<int ,int > FindCent(int v,int fa,int N)
{
    pair<int ,int > Res=MP(inf,-1);
    int S=1,maxsiz=0;
    for(int i=0;i<(int)g[v].size();i++){
        int u=g[v][i];
        if(u!=fa){
            Res=min(Res,FindCent(u,v,N));
            S+=siz[u];
            maxsiz=max(maxsiz,siz[u]);
        }
    }
    maxsiz=max(maxsiz,N-S);
    Res=min(Res,MP(maxsiz,v));
    return Res;
}

The main idea is to find a vertex that after delete it,the size of the max substree of the whole tree is as small as possible...

And the code I got AC:submission here,also in my another account

int Findcent(int v,int fa)
{
    for(int i=0;i<(int)g[v].size();i++){
        int u=g[v][i];
        if(u!=fa && siz[u]>n/2) return Findcent(u,v);
    }
    return v;
}

The second method is found at the grandmaster who took the first place in that contest,and I think the main idea is finding the centroid in the process of continues searching in the substree whose size is larger than half of the tree(aka. n/2).When it stop and return the original parameters from the dfs itself,it is just the centroid because it has no substree whose size is larger than n/2,according to the definition of the centroid of a tree:after deleting it from the whole tree,the max size of substree is as small as possible ,as my friend tell me that if take a centroid as root,the max size of subtree is half the whole tree(n/2).And I think the main idea of these two code are similar...but I got different judgement(also the figure is large so I couldn't realize the difference by this...)

And ....If you have some good materials(in English or in Chinese please;-) about the centroid of a tree,could you please tell me?Thank you very much......

Tags graphs, centroid, #trees

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English -IA- 2017-09-05 14:00:16 744
en2 English -IA- 2017-09-05 10:37:14 1 Tiny change: 'ntroids int a tree,an' -> 'ntroids in a tree,an'
en1 English -IA- 2017-09-05 10:30:53 1866 Initial revision (published)