zoukankan      html  css  js  c++  java
  • POJ1655 Balancing Art

    Balancing Act
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13865   Accepted: 5880

    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”?求重心即可,更新时时刻注意更新最小节点
    一直不理解为啥求重心也叫DP啊!
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5  
     6 inline void read(int &x)
     7 {
     8     x = 0;char ch = getchar();char c = ch;
     9     while(ch > '9' || ch < '0')c = ch, ch = getchar();
    10     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
    11     if(c == '-')x = -x;
    12 }
    13 inline int max(int a, int b){return a > b ? a : b;}
    14 inline int min(int a, int b){return a < b ? a : b;}
    15 
    16 const int INF = 0x3f3f3f3f;
    17 const int MAXN = 200000 + 10;
    18 
    19 struct Edge
    20 {
    21     int u,v,next;
    22 }edge[MAXN << 1];
    23 int t,n,head[MAXN],cnt,b[MAXN],dp[MAXN],ma,g;
    24 inline void insert(int a, int b){edge[++cnt] = Edge{a,b,head[a]};head[a] = cnt;}
    25 
    26 void dfs(int u)
    27 {
    28     int pos, tmp = -1;
    29     dp[u] = 1;
    30     for(pos = head[u];pos;pos = edge[pos].next)
    31     {
    32         int v = edge[pos].v;
    33         if(!b[v])
    34         {
    35             b[v] = true;
    36             dfs(v);
    37             dp[u] += dp[v];
    38             tmp = max(tmp, dp[v]);
    39         }
    40     }
    41     tmp = max(tmp, n - dp[u]);
    42     if(tmp < ma)
    43         ma = tmp,g = u;
    44     if(tmp == ma)
    45         g = min(g, u);
    46 }
    47 
    48 int main()
    49 {
    50     read(t);
    51     register int i,tmp1,tmp2;
    52     for(;t;--t)
    53     {
    54         ma = INF,g = INF;
    55         memset(edge, 0, sizeof(edge));
    56         memset(head, 0, sizeof(head));
    57         cnt = 0;memset(dp, 0, sizeof(dp));
    58         memset(b, 0, sizeof(b));
    59         read(n);
    60         for(i = 1;i < n;++ i)
    61         {
    62             read(tmp1);read(tmp2);
    63             insert(tmp1, tmp2);insert(tmp2, tmp1);
    64         }
    65         b[1] = true;
    66         dfs(1);
    67         printf("%d %d
    ", g, ma);
    68     }
    69     return 0;
    70 }
    View Code
  • 相关阅读:
    腾讯开源 APIJSON 连创五个第一
    最火的分布式 HTAP 数据库 TiDB
    完爆Facebook/GraphQL,APIJSON全方位对比解析(三)-表关联查询
    后端自动化版本管理,再也不用改URL了!
    后端开挂:3行代码写出8个接口!
    3步创建服务端新表及配置
    Activity猫的一生-故事解说Activity生命周期
    APIJSON-以坚持和偏执,回敬傲慢和偏见
    APIJSON,让接口和文档见鬼去吧!
    Android 100多个Styles快速开发布局XML,一行搞定View属性,一键统一配置UI...
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/7123119.html
Copyright © 2011-2022 走看看