zoukankan      html  css  js  c++  java
  • poj 1655 树的重心

    Balancing Act
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13178   Accepted: 5565

    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 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <algorithm>
     6 #include <stack>
     7 #include <queue>
     8 #include <cmath>
     9 #include <map>
    10 #define ll  __int64
    11 #define dazhi 2147483647
    12 #define bug() printf("!!!!!!!")
    13 #define M 200005
    14 using namespace  std;
    15 struct node
    16 {
    17     int from;
    18     int to;
    19 } N[4*M];
    20 int n;
    21 int pre[M];
    22 int nedge=0;
    23 int son[M];
    24 int vis[M];
    25 int l;
    26 int r;
    27 int ans;
    28 int re;
    29 int t;
    30 void add(int f,int t)
    31 {
    32     nedge++;
    33     N[nedge].to=t;
    34     N[nedge].from=pre[f];
    35     pre[f]=nedge;
    36 }
    37 int getnode(int root)
    38 {
    39     vis[root]=1;
    40     son[root]=0;
    41     int temp=0;
    42     for(int i=pre[root];i;i=N[i].from)
    43     {
    44         int x=N[i].to;
    45         if(vis[x]==0)
    46         {
    47             getnode(x);
    48             son[root]+=son[x]+1;
    49             temp=max(temp,son[x]+1);
    50         }
    51     }
    52     temp=max(temp,n-son[root]-1);
    53     if(temp<ans||(temp==ans&&root<re))
    54     {
    55         ans=temp;
    56         re=root;
    57     }
    58 }
    59 int main()
    60 {
    61     scanf("%d",&t);
    62     for(int j=1; j<=t; j++)
    63     {
    64         memset(pre,0,sizeof(pre));
    65         memset(N,0,sizeof(N));
    66         memset(vis,0,sizeof(vis));
    67         nedge=0;
    68         re=M;
    69         ans=M;
    70         scanf("%d",&n);
    71         for(int i=1; i<=n-1; i++)
    72         {
    73             scanf("%d %d",&l,&r);
    74             add(l,r);
    75             add(r,l);
    76         }
    77         getnode(1);
    78         printf("%d %d
    ",re,ans);
    79     }
    80     return 0;
    81 }
  • 相关阅读:
    浅谈数论
    浅谈数论
    bzoj2190 [SDOI2008]仪仗队
    bzoj2190 [SDOI2008]仪仗队
    35.QQ大数据模型
    34.函数指针数组和多线程
    33.函数指针相关问题
    32.分配数组的方式
    31.内存分配四大函数以及栈上分配内存
    30.锯齿数组
  • 原文地址:https://www.cnblogs.com/hsd-/p/6607834.html
Copyright © 2011-2022 走看看