The center of the tree

Source: Internet
Author: User

Definition: At the root of this point, all subtree (not the entire tree itself) are no larger than half the size of the entire tree.

Properties:

Property 1: The distance from all points in the tree to a point and the distance to the center of gravity and is the smallest, if there are two distances and their distances are the same.

Property 2: To connect two trees to a new tree by a certain point, the center of gravity of the new tree must be connected to the path of the original two tree center of gravity.

Property 3: A tree Adds or deletes a node where the center of gravity of the tree moves at most one edge.

Example: POJ 1655

Topic Analysis:

The method of finding the center of gravity of a tree, we find the maximum number of trees connected to each tree, and then the smallest of the largest subtrees of all the nodes is the center of gravity of the tree.

In order to find the maximum subtree of all trees, we can find out the number of nodes below each point, and then ask each subtree to record his number of nodes. Finally, the reverse of the Father node to find the number of sub-trees on the line.

#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include<stack>#include<map>using namespaceStd;typedefLong LongLL;ConstLL INF = 1e9+7;ConstLL MOD = 1e9+7;ConstLL MAXN =20005; Vector<vector<int> >G;intDP[MAXN], NUM[MAXN], N;///The maximum subtree that the point is saved, and Num represents the number of child nodes for that pointvoidDasointroot) {Num[root]=1; intLen =g[root].size ();  for(intI=0; i<len; i++)    {        intv =G[root][i]; if(Num[v] = =0) {DFS (v); Dp[root]=Max (Dp[root], num[v]); Num[root]+ = Num[v];///total number of nodes below}} Dp[root]= Max (Dp[root], nnum[root]);}intMain () {intT, A, B; scanf ("%d", &T);  while(T--) {scanf ("%d", &N);        G.clear (); G.resize (n+3); Memset (DP,-1,sizeof(DP)); memset (num,0,sizeof(num));  for(intI=1; i<n; i++) {scanf ("%d%d", &a, &b);            G[a].push_back (b);        G[b].push_back (a); } DFS (1); intAns =1;  for(intI=2; i<=n; i++)        {            if(Dp[ans] >Dp[i]) ans=i; } printf ("%d%d\n", ans, dp[ans]); }    return 0;}
View Code

The center of the tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.