zoukankan      html  css  js  c++  java
  • POJ 1655 Balancing Act (求树的重心)

    Balancing Act
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7859   Accepted: 3210

    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

    简单的树形DP。

    水题,为了搞树的分治,先来A掉这个题目。

     1 /* ***********************************************
     2 Author        :kuangbin
     3 Created Time  :2013-11-16 22:17:48
     4 File Name     :E:2013ACM专题学习树的分治POJ1655.cpp
     5 ************************************************ */
     6 
     7 #include <stdio.h>
     8 #include <string.h>
     9 #include <iostream>
    10 #include <algorithm>
    11 #include <vector>
    12 #include <queue>
    13 #include <set>
    14 #include <map>
    15 #include <string>
    16 #include <math.h>
    17 #include <stdlib.h>
    18 #include <time.h>
    19 using namespace std;
    20 const int MAXN = 20010;
    21 struct Edge
    22 {
    23     int to,next;
    24 }edge[MAXN*2];
    25 int head[MAXN],tot;
    26 void init()
    27 {
    28     memset(head,-1,sizeof(head));
    29     tot = 0;
    30 }
    31 void addedge(int u,int v)
    32 {
    33     edge[tot].to = v;
    34     edge[tot].next = head[u];
    35     head[u] = tot++;
    36 }
    37 int dp[MAXN],num[MAXN];
    38 int n;
    39 void dfs(int u,int pre)
    40 {
    41     dp[u] = 0;num[u] = 1;
    42     for(int i = head[u];i != -1;i = edge[i].next)
    43     {
    44         int v = edge[i].to;
    45         if(v == pre)continue;
    46         dfs(v,u);
    47         dp[u] = max(dp[u],num[v]);
    48         num[u] += num[v];
    49     }
    50     dp[u] = max(dp[u],n - num[u]);
    51 }
    52 
    53 int main()
    54 {
    55     //freopen("in.txt","r",stdin);
    56     //freopen("out.txt","w",stdout);
    57     int T;
    58     scanf("%d",&T);
    59     int u,v;
    60     while(T--)
    61     {
    62         scanf("%d",&n);
    63         init();
    64         for(int i = 1;i < n;i++)
    65         {
    66             scanf("%d%d",&u,&v);
    67             addedge(u,v);
    68             addedge(v,u);
    69         }
    70         dfs(1,-1);
    71         int ans1 = 1, ans2 = dp[1];
    72         for(int i = 2;i <= n;i++)
    73             if(ans2 > dp[i])
    74             {
    75                 ans1 = i;
    76                 ans2 = dp[i];
    77             }
    78         printf("%d %d
    ",ans1,ans2);
    79     }
    80     return 0;
    81 }
  • 相关阅读:
    Google Code 项目代码托管网站上 Git 版本控制系统使用简明教程
    C/C++预定义宏
    使用 Raspberry Pi 远程桌面
    Vim 中将 tab 自动转换成空格
    DR模式搭建LVS负载均衡
    NAT模式LVS搭建负载均衡集群
    php扩展memached安装
    raw_input与input的区别
    keepalived+lvs搭建高可用负载均衡集群
    使用keepalived搭建nginx高可用
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3427267.html
Copyright © 2011-2022 走看看