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 }
  • 相关阅读:
    mybatis自动生成代码配置文件
    Struts2的类型转换器
    CSS布局自适应高度终极方法
    Winform WebBrowser控件对访问页面执行、改写、添加Javascript代码
    利用using语句解决Lock抛出异常时发生死锁的问题
    Flash与Silverlight终极大比拼
    System.Collections.Specialized.NameValueCollection PostVars
    Hook浏览器控件WebBrowser对WININET.dll的调用
    WebBrowser中打开新页面保留sessionid
    Linksys路由器自动重启加流量
  • 原文地址:https://www.cnblogs.com/zzqc/p/8836017.html
Copyright © 2011-2022 走看看