poj 1655 Balancing Act

Balancing Act
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7412   Accepted: 2994

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: 
poj 1655 Balancing Act_第1张图片
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

POJ Monthly--2004.05.15 IOI 2003 sample task

poj 男人八题1741 的一个小问思路见我的那篇博客。

#include <iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
struct node1//点结构
{
    int sum;//存子树的总结点数
    int maxs;//儿子结点的最大规模。见我最近写的男人八题
} points[20010];
struct node2//边结构
{
    int to;//终点
    node2 *next;
} edge[40010],*head[20010];
int pos[20010],pmaxs[20010];
int cnt,ans,ptr;
void adde(int f,int s)//加边
{
    edge[cnt].next=head[f];
    edge[cnt].to=s;
    head[f]=&edge[cnt++];
}
int ma(int a,int b){ return a>b?a:b; }
void dfs(int fa,int son)//以s为根结点遍历树。计算树的总结点数和子树的最大规模
{
    points[son].maxs=0;
    points[son].sum=1;
    node2 *p=head[son];
    while(p!=NULL)
    {
        if(p->to!=fa)
        {
            dfs(son,p->to);
            points[son].maxs=ma(points[son].maxs,points[p->to].sum);
            points[son].sum+=points[p->to].sum;
        }
        p=p->next;
    }
    pos[ptr]=son;//把算的结果存数组中
    pmaxs[ptr++]=points[son].maxs;
}
void solve(int s)
{
    int i,mins,temp;
    ptr=0;
    dfs(0,s);
    temp=points[s].sum;
    mins=0x3f3f3f3f;
    for(i=0;i<ptr;i++)
    {
        pmaxs[i]=ma(pmaxs[i],temp-points[pos[i]].sum);
        if(pmaxs[i]<mins)//找重心
            mins=pmaxs[i],ans=i;
        else if(pmaxs[i]==mins&&pos[i]<pos[ans])
            ans=i;
    }
}
int main()
{
    int t,i,n,a,b;

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(head,0,sizeof head);
        cnt=0;
        for(i=1;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            adde(a,b);
            adde(b,a);
        }
        solve(1);
        printf("%d %d\n",pos[ans],pmaxs[ans]);
    }
    return 0;
}

看到自己年轻的代码,真的觉得那是很low,这是重写的代码。

#include <iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int N=20010;
struct node
{
    int v;
    node *next;
} ed[N<<1],*head[N];
int son[N],mson[N];
int cnt;
void adde(int u,int v)
{
    ed[cnt].v=v;
    ed[cnt].next=head[u];
    head[u]=&ed[cnt++];
}
void dfs(int fa,int u)
{
    son[u]=1,mson[u]=0;
    for(node *p=head[u];p!=NULL;p=p->next)
    {
        if(p->v==fa)
            continue;
        dfs(u,p->v);
        son[u]+=son[p->v];
        mson[u]=max(mson[u],son[p->v]);
    }
}
int main()
{
    int t,i,n,a,b,ans,ansp;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(head,0,sizeof head);
        cnt=0;
        for(i=1;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            adde(a,b);
            adde(b,a);
        }
        dfs(-1,1);
        ans=mson[1],ansp=1;
        for(i=2;i<=n;i++)
        {
            mson[i]=max(mson[i],n-son[i]);
            if(mson[i]<ans)
                ans=mson[i],ansp=i;
        }
        printf("%d %d\n",ansp,ans);
    }
    return 0;
}


你可能感兴趣的:(c,算法,ACM)