POJ 1655Balancing Act

Posted wuhu-jjj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1655Balancing 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;

structint 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\n",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\n",mark,ans);
    
    return 0;

 

以上是关于POJ 1655Balancing Act的主要内容,如果未能解决你的问题,请参考以下文章

[POJ1655]Balancing Act

POJ - 1655 Balancing Act (树的重心)

POJ1655 Balancing Art

Balancing Act(poj1655)

poj 1655 Balancing Act

POJ 1655Balancing Act