zoukankan      html  css  js  c++  java
  • POJ 1655 Balancing Act 树的重心 基础题

    Balancing Act
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 10347   Accepted: 4285

    Description

    Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the largest tree in the forest T created by deleting that node from T. 
    For example, consider the tree: 

    Deleting node 4 yields two trees whose member nodes are {5} and {1,2,3,6,7}. The larger of these two trees has five nodes, thus the balance of node 4 is five. Deleting node 1 yields a forest of three trees of equal size: {2,6}, {3,7}, and {4,5}. Each of these trees has two nodes, so the balance of node 1 is two. 

    For each input tree, calculate the node that has the minimum balance. If multiple nodes have equal balance, output the one with the lowest number. 

    Input

    The first line of input contains a single integer t (1 <= t <= 20), the number of test cases. The first line of each test case contains an integer N (1 <= N <= 20,000), the number of congruence. The next N-1 lines each contains two space-separated node numbers that are the endpoints of an edge in the tree. No edge will be listed twice, and all edges will be listed.

    Output

    For each test case, print a line containing two integers, the number of the node with minimum balance and the balance of that node.

    Sample Input

    1
    7
    2 6
    1 2
    1 4
    4 5
    3 7
    3 1
    

    Sample Output

    1 2

    Source

     
     
     
     
    题意:
    给出一棵树,求出树的重心和该重心的最大的子树的节点个数。
     
    重心:
    对于一棵树,若删除某一个节点,这棵树会分成一棵或者多棵子树。
    定义树的大小:这棵树的节点个数。
    若一个节点删除后分成的所有子树的大小都不超过原树的1/2,则该节点称之为重心。
     
    注意:一棵树的重心可能不止一个。
     
    这道题就是简单的求树的重心,和该重心的最大的子树的大小。
     
    dfs一遍即可。
     
    siz[i]以i为根的树的大小
    son[i] i的子树中大小最大的子树的根的节点编号。
    (和树链剖分一样)
     
    然后dfs。
     
     
     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 
     5 using namespace std;
     6 
     7 const int maxn=20000+5;
     8 const int inf=0x3f3f3f3f;
     9 
    10 struct Edge
    11 {
    12     int to,next;
    13 }edge[maxn<<1];
    14 
    15 int head[maxn];
    16 int tot;
    17 int son[maxn];
    18 int siz[maxn];
    19 int ans[maxn];
    20 
    21 void init()
    22 {
    23     memset(head,-1,sizeof(head));
    24     tot=1;
    25     memset(ans,0,sizeof(ans));
    26     memset(son,-1,sizeof(son));
    27 }
    28 
    29 void addedge(int u,int v)
    30 {
    31     edge[tot].to=v;
    32     edge[tot].next=head[u];
    33     head[u]=tot++;
    34 }
    35 
    36 void dfs(int u,int fa)
    37 {
    38     siz[u]=1;
    39     for(int i=head[u];~i;i=edge[i].next)
    40     {
    41         int v=edge[i].to;
    42         if(v==fa)
    43             continue;
    44         dfs(v,u);
    45         siz[u]+=siz[v];
    46         if(son[u]==-1||siz[v]>siz[son[u]])
    47             son[u]=v;
    48     }
    49 }
    50 
    51 int solve(int n)
    52 {
    53     dfs(1,-1);
    54 
    55     for(int i=1;i<=n;i++)
    56         ans[i]=max(siz[son[i]],n-siz[i]);
    57     int id=1;
    58     for(int i=2;i<=n;i++)
    59     {
    60         if(ans[i]<ans[id])
    61             id=i;
    62     }
    63     return id;
    64 }
    65 
    66 int main()
    67 {
    68     int test;
    69     scanf("%d",&test);
    70     while(test--)
    71     {
    72         int n;
    73         scanf("%d",&n);
    74         init();
    75 
    76         for(int i=1;i<n;i++)
    77         {
    78             int u,v;
    79             scanf("%d%d",&u,&v);
    80             addedge(u,v);
    81             addedge(v,u);
    82         }
    83 
    84         int id=solve(n);
    85 
    86         printf("%d %d
    ",id,ans[id]);
    87     }
    88 
    89     return 0;
    90 }
    View Code
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    出差公干,人在北京,欢迎骚扰
    义乌江东货运市场托运指南
    哥伦比亚拖鞋批发,60双起批发仅售11,现货3240双,义乌给力商务有限公司诚意供货,发韵达或指定物流
    员工心态 老板心态 学生心态
    淘宝卖家
    开源问题系统
    当一回"青年导师"
    义乌工商学院到给力百货有多远,怎么走?
    我也八卦,姚晨和它男人离婚的事,意料之外,情理之中,吼吼,我是在迅雷看看的评论中看到的
    新浪车模程序写得很有意思
  • 原文地址:https://www.cnblogs.com/-maybe/p/4590674.html
Copyright © 2011-2022 走看看