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

    Balancing Act

    Time Limit: 1000ms
    Memory Limit: 65536KB
    This problem will be judged on PKU. Original ID: 1655
    64-bit integer IO format: %lld      Java class name: Main
     
    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 <cstring>
     4 using namespace std;
     5 const int maxn = 20010;
     6 struct arc {
     7     int to,next;
     8     arc(int x = 0,int y = -1) {
     9         to = x;
    10         next = y;
    11     }
    12 } e[maxn<<1];
    13 int head[maxn],siz[maxn],tot,n;
    14 void add(int u,int v) {
    15     e[tot] = arc(v,head[u]);
    16     head[u] = tot++;
    17 }
    18 int ret,idx;
    19 void dfs(int u,int fa) {
    20     siz[u] = 1;
    21     int tmp = 0;
    22     for(int i = head[u]; ~i; i = e[i].next) {
    23         if(e[i].to == fa) continue;
    24         dfs(e[i].to,u);
    25         tmp = max(siz[e[i].to],tmp);
    26         siz[u] += siz[e[i].to];
    27     }
    28     tmp = max(tmp,n - siz[u]);
    29     if(tmp < ret || tmp == ret && u < idx) {
    30         ret = tmp;
    31         idx = u;
    32     }
    33 }
    34 int main() {
    35     int kase,u,v;
    36     scanf("%d",&kase);
    37     while(kase--) {
    38         scanf("%d",&n);
    39         memset(head,-1,sizeof head);
    40         idx = ret = n<<1;
    41         tot = 0;
    42         for(int i = 1; i < n; ++i) {
    43             scanf("%d%d",&u,&v);
    44             add(u,v);
    45             add(v,u);
    46         }
    47         dfs(1,-1);
    48         printf("%d %d
    ",idx,ret);
    49     }
    50     return 0;
    51 }
    View Code
  • 相关阅读:
    [SUCTF 2019]Pythonginx
    Buuctf-RSA1
    [网鼎杯 2020 朱雀组]phpweb
    [BJDCTF2020]ZJCTF,不过如此
    CTFHub-信息泄露
    vaex读取和处理大型文件的方法
    "生猪数据统计分析系统"----帮助文档
    法治理论1
    很短暂,很喜欢,很遗憾
    「CF1513E Cost Equilibrium」
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4737664.html
Copyright © 2011-2022 走看看