zoukankan      html  css  js  c++  java
  • POJ 1655 Balancing Act

    Balancing Act
     

    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

    题意:
      树的重心定义为:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡.

     思路:

      根据定义来

    AC代码:

     1 # include <iostream>
     2 # include <cstring>
     3 # include <cstdio>
     4 
     5 using namespace std;
     6 
     7 const int MAX = 20010;
     8 
     9 struct node
    10 {
    11     int to;
    12     int next;
    13 }tree[MAX * 2];
    14 int head[MAX];
    15 int tol;
    16 int dp[MAX], num[MAX];
    17 int n;
    18 
    19 void add(int a, int b)
    20 {
    21     tree[tol].to = b;
    22     tree[tol].next = head[a];
    23     head[a] = tol++;
    24 }
    25 
    26 void dfs(int root, int f)
    27 {
    28     dp[root] = 0;
    29     num[root] = 1;
    30     for(int i = head[root]; i != -1; i = tree[i].next)
    31     {
    32         int son = tree[i].to;
    33         if(son == f)
    34             continue;
    35         dfs(son, root);
    36         dp[root] = max(dp[root], num[son]);
    37         num[root] += num[son];
    38     }
    39     dp[root] = max(dp[root], n - num[root]);
    40 }
    41 
    42 int main()
    43 {
    44     int T;
    45     scanf("%d", &T);
    46     while(T--)
    47     {
    48         tol = 0;
    49         memset(head, -1, sizeof(head));
    50         scanf("%d", &n);
    51         int a, b;
    52         for(int i = 1; i < n; i++)
    53         {
    54             scanf("%d%d", &a, &b);
    55             add(a, b);
    56             add(b, a);
    57         }
    58         dfs(1, -1);
    59         
    60         int Min1 = 1, Min2 = dp[1];
    61         for(int i = 2; i <= n; i++)
    62         {
    63             if(Min2 > dp[i])
    64             {
    65                 Min2 = dp[i];
    66                 Min1 = i;
    67             }
    68         }
    69         printf("%d %d
    ", Min1, Min2);
    70         
    71     }
    72     
    73     return 0;
    74 }
    View Code
    生命不息,奋斗不止,这才叫青春,青春就是拥有热情相信未来。
  • 相关阅读:
    SQL游标操作每隔5分钟时间段数据统计信息
    win64位操作系统下安装pl/sql developer 并登录连接到oracle12c
    分科目统计每科前三名的学生
    merge源表数据移植到目标表新表数据中
    sqlserver表分区
    用SqlBulkCopy批量插入数据到SqlServer数据库表中
    SQL server插入数据后,如何获取自增长字段的值?
    Java创建线程的三种方式
    Java用户线程和守护线程
    Java虚拟机详解
  • 原文地址:https://www.cnblogs.com/lyf-acm/p/5821968.html
Copyright © 2011-2022 走看看