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

    http://poj.org/problem?id=1655

    Balancing Act
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8295   Accepted: 3416

    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
    
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int len,dp[40005],head[40005],n;
    int val[40005],sum[40005];
    struct node
    {
        int now,next;
    }tree[40005];
    void add(int x,int y)
    {
       tree[len].now=y;
       tree[len].next=head[x];
       head[x]=len++;
    }
    void dfs(int root,int p)
    {
        int i,son,temp=0;
          sum[root]=1;
         for(i=head[root];i!=-1;i=tree[i].next)
        {
            //printf("i=%d
    ",i);
             son=tree[i].now;
              if(son==p)
              {
                continue;
              }
           //   printf("son=%d
    ",son);
               dfs(son,root);
               sum[root]+=sum[son];
              // printf("root,sum[%d]=%d,son,sum[%d]=%d
    ",root,sum[root],son,sum[son]);
               temp=max(temp,sum[son]);
              // printf("temp=%d
    ",temp);
        }
        //printf("ii=%d,root=%d
    ",i,root);
        //printf("tttemp=%d
    ",temp);
        dp[root]=max(temp,n-sum[root]);
          //printf("dp=%d
    ",dp[root]);
    }
    int main()
    {
        int t,a,b,i,q;
        cin>>t;
        while(t--)
        {
                len=0;
            memset(dp,0,sizeof(dp));
            memset(head,-1,sizeof(head));
               cin>>n;
            for(i=1;i<n;i++)
            {
             scanf("%d%d",&a,&b);
               add(a,b);
               add(b,a);
            }
            dfs(1,-1);
            int ans=1;
           // for(i=1;i<=n;i++)
            //   printf("dp=%d
    ",dp[i]);
            for(i=2;i<=n;i++)
            {
    
                if(dp[ans]>dp[i]) ans=i;
            }
            printf("%d %d
    ",ans,dp[ans]);
        }
        return 0;
    }
    
  • 相关阅读:
    Linux YUM (Yellowdog Updater, Modified) Commands for Package Management
    awk命令例子详解
    sed命令例子详解
    浅谈XX系统跨平台迁移(测试环境)
    Postgres常用命令之增、删、改、查
    Postgres主备切换
    Postgres配置主从流复制
    Crontab定时任务
    GIL全局锁测试
    python try exception finally记录
  • 原文地址:https://www.cnblogs.com/cancangood/p/3671864.html
Copyright © 2011-2022 走看看