【树的重心】 POJ 1655 Balancing Act

Balancing Act
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11911 Accepted: 5019

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

POJ Monthly--2004.05.15 IOI 2003 sample task


最近在做树形DP的题目,但是队长给我挂的树形DP好像有什么不太对啊?!第三题是树分治啊!喂喂是不是跳的有点过分了呀<手动掀桌>。


嘛,题目还是要做的,不过在做树分治之前我需要先把树的重心掌握,于是顺便找了下别人写的题解,在ACdreamer的blog上发现了这道题。

树的重心,嘛,就是说一个类似于一个稳定的中点(几何没学好概念忘记啦OvO)。在树上的定义也差不多,意思是该树的子树中结点数最多的节点数最少,这句话多读几遍应该能理解=w=


要找树的重心,我们需要知道它的所有的直接子树上的节点数目,这一点用Dfs便能轻松实现,然而如果是一棵无根树,那么对每个点进行查找的时候就会产生重叠,导致复杂度升高。所以我们将其看作一棵有根树,然后从根节点开始查找所有的子节点的子树(递归实现),父节点的子树则用总点数减去(该点子节点和+1)来判断。


题目虽然简单,不过对于理解树形DP还是有一定的帮助。


题目地址:http://poj.org/problem?id=1655

题目代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;

const int MAXN=40010;

int n;

int u[MAXN],v[MAXN],nex[MAXN];
int fir[MAXN];
bool vis[MAXN];
int e_max;

void add_edge(int a,int b)
{
    int e=e_max++;
    u[e]=a;
    v[e]=b;
    nex[e]=fir[a];
    fir[a]=e;
}

int dp[MAXN];
int ans,maxans;

void Dfs(int now)
{
    vis[now]=1;
    dp[now]=0;
    int tmp=0;
    for(int e=fir[now];~e;e=nex[e])
    {
        int to=v[e];
        if(vis[to]) continue;

        Dfs(to);
        tmp=max(dp[to]+1,tmp);
        dp[now]+=dp[to]+1;
        //printf("now:%d %d\n",now,dp[now]);
    }
    tmp=max(n-dp[now]-1,tmp);
    //printf("now:%d %d\n",now,tmp);

    if(tmp<maxans)
    {
        maxans=tmp;
        ans=now;
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);

        e_max=0;
        memset(dp,0,sizeof dp);
        memset(fir,-1,sizeof fir);
        memset(vis,0,sizeof vis);
        for(int i=0;i<n-1;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            add_edge(a,b);
            add_edge(b,a);
        }

        ans=1;maxans=n;
        Dfs(1);

        printf("%d %d\n",ans,maxans);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值