POJ - 1655 Balancing Act (树的重心)

题目链接:https://vjudge.net/problem/POJ-1655

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

题意:就是去掉树上的一个节点,看看剩下的子树中节点总数最大的是多少,
然后在这些最大值中求一个最小值,如果有多个点都是最小值,那么找一个序号最小的节点,
输出节点号,和最小值。

思路:定义dp[i]是以节点i为根的树的节点总数,则去掉节点i后的树的节点总数为(n-dp[i]),从小到大枚举节点i,最后取max(dp[i],n-dp[i])即可。

AC代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=20000+10;
vector<int>p[maxn];
int dp[maxn],n,min_node,max_sum;
void DFS(int u,int fa){
    int sum=0;
    dp[u]=1;
    for(int i=0;i<p[u].size();i++){
        int v=p[u][i];
        if(v!=fa){
            DFS(v,u);
            dp[u]+=dp[v];
            sum=max(sum,dp[v]);
        }
    }
    sum=max(sum,n-dp[u]);
    if(sum<max_sum)
    {
        max_sum=sum;
        min_node=u;
    }
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int u,v;
        scanf("%d",&n);
        for(int i=1;i<=n;i++) p[i].clear();
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&u,&v);
            p[u].push_back(v);
            p[v].push_back(u);
        }
        max_sum=maxn;
        DFS(1,0);
        printf("%d %d\n",min_node,max_sum);    
    }
}

  

 

posted @   ccsu_dj辉  阅读(231)  评论(3编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 上周热点回顾(1.20-1.26)
点击右上角即可分享
微信分享提示