zoukankan      html  css  js  c++  java
  • POJ 1655 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.
     
    题目大意:给一棵树,问删掉哪个结点后,剩下的树的最大结点数最小。
    思路:DFS一次即可求出所有点的子树大小size,顺便算出每个点最大的子树maxSize。那么对于每个点删掉之后,剩下的树的最大结点数就是max(maxSize, n - size),前面就是它的所有子树的最大size,后面就是删掉这个点后,它父亲所在的树的大小。
    在研究树的分治之前先来补一条水题。。。这也算DP- -?
     
    代码(47MS):
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 const int MAXN = 20010;
     8 const int MAXE = 40010;
     9 const int INF = 0x7fff7fff;
    10 
    11 int head[MAXN], size[MAXN], maxSize[MAXN], f[MAXN];
    12 int to[MAXE], next[MAXE];
    13 int n, ecnt;
    14 
    15 void init() {
    16     memset(head, -1, sizeof(head));
    17     ecnt = 0;
    18 }
    19 
    20 void add_edge(int u, int v) {
    21     to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
    22     to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
    23 }
    24 
    25 void dfs(int u) {
    26     maxSize[u] = 0;
    27     size[u] = 1;
    28     for(int p = head[u]; ~p; p = next[p]) {
    29         int &v = to[p];
    30         if(v == f[u]) continue;
    31         f[v] = u;
    32         dfs(v);
    33         size[u] += size[v];
    34         maxSize[u] = max(maxSize[u], size[v]);
    35     }
    36 }
    37 
    38 int main() {
    39     int T;
    40     scanf("%d", &T);
    41     while(T--) {
    42         init();
    43         scanf("%d", &n);
    44         int u, v;
    45         for(int i = 1; i < n; ++i) {
    46             scanf("%d%d", &u, &v);
    47             add_edge(u, v);
    48         }
    49         dfs(1);
    50         int pos, maxd = INF;
    51         for(int i = 1; i <= n; ++i) {
    52             if(max(maxSize[i], n - size[i]) < maxd) {
    53                 pos = i;
    54                 maxd = max(maxSize[i], n - size[i]);
    55             }
    56         }
    57         printf("%d %d
    ", pos, maxd);
    58     }
    59 }
    View Code
  • 相关阅读:
    PHPNow升级PHP版本为5.3.5的方法(转)
    常用Raspberry Pi周边传感器的使用教程(转)
    Raspberry pi 使用python+pySerial实现串口通信(转)
    树莓派相关-树莓派串口配置方法(转)
    树莓派折腾---红外探测
    String.format和MessageFormat.format的对比用法
    使用FastJson从json串中根据key获取value
    使用HttpClient配置代理服务器模拟浏览器发送请求调用接口测试
    gradle查看依赖关系并写入到文本文件的命令
    使用 "java -jar"命令启动jar包时报不支持的jdk版本异常
  • 原文地址:https://www.cnblogs.com/oyking/p/3408852.html
Copyright © 2011-2022 走看看