zoukankan      html  css  js  c++  java
  • poj-1655-树的重心

    Balancing Act
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 15526   Accepted: 6575

    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

    求给定的一颗树的平衡点,就是这个点的最大的一颗子树森林的节点数尽量小。想到树形dp,但是子节点的最大子树可能来自
    父亲,但是来自父亲的那棵树的节点数我们可以通过计算出当前节点为根的节点数再用N减去就好了,显然可以递归的时候顺便算出来。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<vector>
     5 using namespace std;
     6 vector<int>g[20010];
     7 int f[20010],num[20010],n;
     8 void dfs(int u,int fa){
     9     f[u]=0,num[u]=1;
    10     for(int i=0;i<g[u].size();++i){
    11         if(g[u][i]==fa) continue;
    12         dfs(g[u][i],u);
    13         f[u]=max(f[u],num[g[u][i]]);
    14         num[u]+=num[g[u][i]];
    15     }
    16     f[u]=max(f[u],n-num[u]);
    17 }
    18 int main()
    19 {
    20     int t,m,i,j;
    21     int u,v;
    22     cin>>t;
    23     while(t--){
    24         cin>>n;
    25         for(i=1;i<=n;++i) g[i].clear();
    26         for(i=1;i<n;++i){
    27             scanf("%d%d",&u,&v);
    28             g[u].push_back(v);
    29             g[v].push_back(u);
    30         }
    31         dfs(1,0);
    32         int ans1=0,ans2=999999;
    33         for(i=1;i<=n;++i){
    34             if(f[i]<ans2){
    35                 ans1=i;
    36                 ans2=f[i];
    37             }
    38         }
    39         cout<<ans1<<' '<<ans2<<endl;
    40     }
    41     return 0;
    42 }
  • 相关阅读:
    AutoCAD.Net/C#.Net QQ群:193522571 resultbuffer 中的typedvalue
    AutoCAD.Net/C#.Net QQ群:193522571 32位进程无法访问64位进程模块,解决getprocesses方法对32位无效的问题
    AutoCAD.Net/C#.Net QQ群:193522571 随机数
    AutoCAD.Net/C#.Net QQ群:193522571:取得当前方法名、父方法名
    每次打开office2007都会弹出安装autocad2007,如何解决?
    在自定义控件中,定义枚举类型需要使其首项默认值为0
    VS整死了,属性惹的祸
    委托的使用,排序
    为C#自定义控件添加自定义事件
    C#获取字符串宽度像素
  • 原文地址:https://www.cnblogs.com/zzqc/p/8836017.html
Copyright © 2011-2022 走看看