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

    Balancing Act
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8048   Accepted: 3322

    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[i][1] 统计自身及子节点的 总个数
     dp[i][0] 统计 子节点的最大个数
     假如,删去i 节点,那么剩下的森林中,会有怎么样情况。
     它的父亲节点相连的一棵树,自身相连的子树。枚举他们的大小就可以了。
     
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<queue>
     6 #include<vector>
     7 #include<algorithm>
     8 using namespace std;
     9 
    10 vector<int>Q[20002];
    11 int num[20002];
    12 int dp[20002][2];
    13 //DP[I][0] 统计子节点的最大个数
    14 //DP[I][1] 统计 自身及子节点 的总个数
    15 bool use[20002];
    16 void add(int x,int y)
    17 {
    18     Q[x].push_back(y);
    19     num[x]++;
    20 }
    21 int Max(int x,int y)
    22 {
    23     return x>y? x:y;
    24 }
    25 void dfs(int k)
    26 {
    27     int i,t,cur=0;
    28     use[k]=true;
    29     dp[k][1]=1;
    30     for(i=0;i<num[k];i++)
    31     {
    32         t=Q[k][i];
    33         if(use[t]==true)continue;
    34         dfs(t);
    35 
    36         if(cur<dp[t][1])
    37             cur=dp[t][1];
    38         dp[k][1]=dp[k][1]+dp[t][1];
    39     }
    40     dp[k][0]=cur;
    41 }
    42 int main()
    43 {
    44     int T;
    45     int i,n,x,y,cur,hxl,tom;
    46     scanf("%d",&T);
    47     while(T--)
    48     {
    49         scanf("%d",&n);
    50         for(i=0;i<=n;i++)
    51         {
    52             num[i]=0;
    53             Q[i].clear();
    54         }
    55         memset(dp,0,sizeof(dp));
    56         memset(use,false,sizeof(use));
    57 
    58         for(i=1;i<n;i++)
    59         {
    60             scanf("%d%d",&x,&y);
    61             add(x,y);
    62             add(y,x);
    63         }
    64         dfs(1);
    65         hxl=20005;
    66         for(i=1;i<=n;i++)
    67         {
    68             cur=Max(dp[i][0],n-dp[i][1]);
    69             if(cur<hxl)
    70             {
    71                 hxl=cur;
    72                 tom=i;
    73             }
    74         }
    75         printf("%d %d
    ",tom,hxl);
    76     }
    77     return 0;
    78 }
  • 相关阅读:
    DOS常用命令
    SQL查询优化:详解SQL Server非聚集索引(转载)
    SQLServer索引
    SQL注入技术专题—由浅入深【精华聚合贴】
    DVWA 1.9 通关秘籍
    高手养成计划基础篇-Linux第二季
    【摩斯电码】我是如何通过一张小纸条渗透进了妹子的心
    阿里旗下蚂蚁金服副总:有望3-5年内消灭伪基站
    【病毒分析】对一个vbs脚本病毒的分析
    【安全热点】阿里巴巴月饼门,观点两极化,孰对孰错?
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3599863.html
Copyright © 2011-2022 走看看