The center of gravity and the diameter of the tree

The center of gravity of the tree

  1. Definition: The center of gravity of the tree also called tree of the center of mass. Find a point, the largest sub-tree nodes least all of its sub-tree, then the tree's center of gravity is the point, after deleting the center of gravity to produce more trees as balanced as possible.
  2. nature
  • Tree in the distance and all points of a point, and the distance to the center of gravity is minimal, if there are two distances and their distance and the same.
  • The two trees are connected by an edge, the new center of gravity of the tree the two trees in the center of gravity of the original connection.
  • A tree add or delete a node, moving the center of gravity of the tree at most one edge position.
  • The center of gravity of a tree has at most two, and adjacent.
    Focus 3.find tree
  • The main idea with DP
  • root center of gravity, f [x] x to record the maximum number of nodes as the root node of its subtree, sum [x] x is a root node which records all the sub-tree nodes
 void getroot(int now,int fa)
{
    f[now]=0; sum[now]=1;
    for (int i=head[now]; i!=-1; i=e[i].nx)
        if (e[i].v!=fa)
        {
            getroot(e[i].v,now);
            sum[now]+=sum[e[i].v];
            f[now]=max(f[now],sum[e[i].v]);
        }
    f[now]=max(f[now],n-sum[now]);//此时以now为根节点,now的父节点为它的一个子树,n-sum[now]为now父节点的节点总数
    if (f[now]<ans) {ans=f[now]; root=now;}
    if (f[now]==ans&&now<root) root=now;
}

4. a template question: poj1655

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N=20005;
int T,n,tot,root,ans,head[N],f[N],sum[N];
 struct ss
{
    int v,nx;
}e[N*2];

 void add(int u,int v)
{
    tot++; e[tot].v=v; e[tot].nx=head[u]; head[u]=tot;
}
 void getroot(int now,int fa)
{
    f[now]=0; sum[now]=1;
    for (int i=head[now]; i!=-1; i=e[i].nx)
        if (e[i].v!=fa)
        {
            getroot(e[i].v,now);
            sum[now]+=sum[e[i].v];
            f[now]=max(f[now],sum[e[i].v]);
        }
    f[now]=max(f[now],n-sum[now]);
    if (f[now]<ans) {ans=f[now]; root=now;}
    if (f[now]==ans&&now<root) root=now;
}

 int main()
{
    scanf("%d",&T);
    while (T--)
    {
        memset(head,-1,sizeof(head)); tot=0;
        memset(f,0,sizeof(f)); memset(sum,0,sizeof(sum));
        int u,v;
        scanf("%d",&n); 
        ans=0x3f3f3f3f;
        for (int i=1; i<n; i++)
        {
            scanf("%d%d",&u,&v);
            add(u,v); add(v,u);
        }
        getroot(1,-1);
        printf("%d %d\n",root,f[root]);
    }
    return 0;
}

Simple longest road tree: the tree diameter

  • Take any point after the BFS, find the farthest point x, x starts from the farthest point BFS find another y, x y is the distance to the diameter of the tree
  • poj2631
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int N=20005;
int head[N],tot,d[N],q[N],ans,num;
bool flag[N];
 struct ss
{
    int v,w,nx;
}f[N];

 void add(int u,int v,int w)
{
    tot++; f[tot].v=v; f[tot].w=w; f[tot].nx=head[u]; head[u]=tot;
}
 int bfs(int s)
{
    memset(flag,0,sizeof(flag));
    flag[s]=1; d[s]=0; q[1]=s; ans=-1;
    int l=0,r=1; 
    while (l<r)
    {
        int x=q[++l];
        if (d[x]>ans) ans=d[x],num=x;
        for (int i=head[x]; i!=-1; i=f[i].nx)
            if (!flag[f[i].v])
            {
                flag[f[i].v]=true;
                d[f[i].v]=d[x]+f[i].w;
                q[++r]=f[i].v;      
            }
    }
    return num;
}

 int main()
{
    int u,v,w; tot=0;
    memset(head,-1,sizeof(head));
    while (scanf("%d%d%d",&u,&v,&w)!=EOF) add(u,v,w),add(v,u,w);
    bfs(bfs(1));
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/lyxzhz/p/11616972.html