zoukankan      html  css  js  c++  java
  • 【POJ 1655】Balancing Act

    Balancing Act
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 18593   Accepted: 7849

    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

    [Submit]   [Go Back]   [Status]   [Discuss]

    Home Page   Go Back  To top

    题解:本应该快乐的树形DP,和标程没啥区别,甚至连oo都改了差不多了

    我就是超时,标程对的,无语,不知道哪错啦。

    正解:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int MAX=20005;
    
    int visit[MAX],num[MAX],head[MAX],ans;
    int n,mark,k,sum,sizemin;
    
    struct{int v,next;}e[MAX*2];
    
    void  addedge(int u,int v)
    {
        k++;
        e[k].v=v;
        e[k].next=head[u];
        head[u]=k;
        return;
    }
    
    void dfs(int u)
    {// cout<<"L"<<endl;
        int i,t,min;
        visit[u]=1;
        num[u]=1;min=-1;
        for(i=head[u];i;i=e[i].next)
        {
            t=e[i].v;
            if(!visit[t])
            {
                dfs(t);
                num[u]+=num[t];
                min=max(min,num[t]);
            }
        }
        min=max(min,n-num[u]);
        if(min<sizemin)
        {
            ans=min;
            mark=u;
            sizemin=min;
        }
    
       return ;
    }
    
    int main()
    {
        int CASE,u,v,i;
        scanf("%d",&CASE);
        while(CASE--)
        {
            scanf("%d",&n);
            k=0;
            memset(head,0,sizeof(head));
            for(i=1;i<n;i++)
            {
               scanf("%d%d",&u,&v);
               addedge(u,v);
               addedge(v,u);
            }
     // cout<<"no"<<endl;
            memset(num,0,sizeof(num));
            memset(visit,0,sizeof(visit));
            k=0;
            sizemin=MAX+10;
            dfs(1);
            printf("%d %d
    ",mark,ans);
    
    
        }
        return 0;
    }

     我的代码,找了一个小时还是错,。。。。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    typedef long long ll;
    using namespace std;
    const int N=40005;
    const int oo=0x3f3f3f3f;
    int ycll,n,cnt,head[N],vis[N],num[N],mn,ans,mark; 
    struct node{
        int v;
        int next;
    }e[N]; 
    void add(int u,int v){
        cnt++; e[cnt].v=v;
        e[cnt].next=head[u];
        head[u]=cnt;
    }
    void dfs(int u){
        vis[u]=1;
        num[u]=1;
        int jjj=-1;
        for(int i=head[u];i;i=e[i].next){
            int dv=e[i].v;
            if(!vis[dv]){
                dfs(dv); num[u]+=num[dv];
                jjj=max(jjj,num[dv]);
            }
        }
        jjj=max(jjj,n-num[u]);
        if(jjj<mn) 
           { ans=jjj; mark=u; mn=oo; }
    }
    int main(){
        freopen("1655.in","r",stdin);
        freopen("1655.out","w",stdout);
        scanf("%d",&ycll);
        while(ycll--){
            scanf("%d",&n);
            cnt=0; 
            memset(num,0,sizeof(num));
            memset(vis,0,sizeof(vis));
            memset(head,0,sizeof(head));
            int x,y;
            for(int i=1;i<n;i++){
                scanf("%d %d",&x,&y);
                add(x,y); add(y,x);
            }
            cnt=0; mn=N+10; dfs(1);
            printf("%d %d
    ",mark,ans);
        }
        return 0;
    }
  • 相关阅读:
    系统可靠性测试
    系统可靠性的技术设计
    可靠性测试的基础知识——软件可靠性测试
    可靠性测试的基础知识——可靠性的计算方法
    python 中英文时间转换
    Python之日期与时间处理模块(date和datetime)
    使用Python操作Redis
    scrapy操作指南
    爬虫中什么是requests
    面向对象的自我总结
  • 原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11241650.html
Copyright © 2011-2022 走看看