码迷,mamicode.com
首页 > 其他好文 > 详细

poj-1655 Balancing Act(树的重心+树形dp)

时间:2016-06-25 13:48:33      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:

Balancing Act

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11845   Accepted: 4993

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

题意:

问给的一棵树的重心是哪个节点以及把这个节点去掉后连通块节点个数的最大值;

思路:

dfs,找出所有节点的子树节点的个数;再找出去掉这个节点后最大连通块的节点数更新答案就好了;

AC代码:

//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<0||CH>9;F= CH==-,CH=getchar());
    for(num=0;CH>=0&&CH<=9;num=num*10+CH-0,CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + 0);
    putchar(\n);
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=2e5+10;
const int maxn=1005;

int n,son[N],ans,num;
vector<int>ve[N];

void dfs(int x,int fa)
{
    int len=ve[x].size(),mmax=0;
    son[x]=1;
    for(int i=0;i<len;i++)
    {
        int y=ve[x][i];
        if(y==fa)continue;
        dfs(y,x);
        son[x]+=son[y];
        if(son[y]>mmax)
            mmax=son[y];
    }

    int d=max(mmax,n-son[x]);
    if(d<=num)
    {
        if(d==num)
        {
            if(x<ans)ans=x;
        }
        else ans=x;
        num=d;
    }
}

int main()
{
    int t;
    read(t);
    while(t--)
    {
        read(n);
        for(int i=0;i<=n;i++)ve[i].clear();
        int x,y;
        for(int i=1;i<n;i++)
        {
            read(x);read(y);
            ve[x].push_back(y);
            ve[y].push_back(x);
        }
        ans=1;
        num=50000000;
        dfs(1,-1);
        cout<<ans<<" "<<num<<"\n";
    }
        return 0;
}

 

poj-1655 Balancing Act(树的重心+树形dp)

标签:

原文地址:http://www.cnblogs.com/zhangchengc919/p/5616201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!