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......








Auto comment: topic has been updated by -IA- (previous revision, new revision, compare).
"submission here,in my another account". You know that it is prohibited to have multiple accounts, right ?
Sorry for that... And BTW I'm sure I didn't use multiple accounts to cheat in any exams.Have multiple account is just because I sometimes feel not good and don't want to decrease my rating in certain account...
In you first logic, if we suppose that siz[u] does not contain 'u', then variable maxsize into the loop keeps that maximum subsize. Also variable 'S' keeps the sum of them. After you take the substraction N-S. That means that you substract from all the nodes all the subtrees, but you forget to substract 'u' itself. So try to replace it with N-S-1.
(I have not tested it and I am not sure if my point above is correct. So I can be wrong).
In the beginning S is equal to 1.
That is right! My bad!
Oh...it seemed that you thought the first code is right? But it surely works differently... Thank you as well...
Um...
The snippets you provided are pretty different aren't they? This is not a case of some subtle difference that is not obvious or intuitive behavior. If you wrote them yourself, surely you can explain how they work? I'm sure there are many people who'd love to help you but can't be assed to parse what you might mean by
sz[g[i][v]][u]++;or some similar equally blotched statement.I wrote them by myself after I understood what they meant...I know it looks different by code between these two method...but I think the main idea is similar,more concretely...the first code is to find a vertex that after delete it,the size of the max substree of the whole tree is as small as possible(as I mentioned after the code...)
Oh maybe I didn't explain the meaning of the second code,sorry...
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...)
Can some other people help me please...?Thank you in advance...