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;
    }
    
  • 相关阅读:
    基础总结深入:数据类型的分类和判断(数据、内存、变量) 对象 函数 回调函数 IIFE 函数中的this 分号
    BOM 定时器 通过修改元素的类来改变css JSON
    事件 事件的冒泡 事件的委派 事件的绑定 事件的传播
    DOM修改 使用DOM操作CSS
    包装类 Date Math 字符串的相关的方法 正则表达式 DOM DOM查询
    数组 call()、apply()、bind()的使用 this arguments
    autocad 二次开发 最小包围圆算法
    win10 objectarx向导在 vs2015中不起作用的解决办法
    AutoCad 二次开发 jig操作之标注跟随线移动
    AutoCad 二次开发 文字镜像
  • 原文地址:https://www.cnblogs.com/cancangood/p/3671864.html
Copyright © 2011-2022 走看看