题目链接:http://poj.org/problem?id=1655

Balancing Act
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19110   Accepted: 8083

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

树的重心:
找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡。
树的重心的性质:
1.树中所有点到某个点的距离和中,到重心的距离和是最小的;如果有两个重心,那么他们的距离和一样。
2.把两个树通过一条边相连得到一个新的树,那么新的树的重心在连接原来两个树的重心的路径上。
3.把一个树添加或删除一个叶子,那么它的重心最多只移动一条边的距离。
题目大意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的.

看代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int maxn=2e4+5;
const int INF=1e9+7;
int head[maxn<<1];
int sonnum[maxn<<1],sonmax[maxn];
int cnt;
int N;
struct Edge
{int next,to;//next表示同起点的下一条边的下标  to表示这条边的终点
}e[maxn<<1];
void add_edge(int u,int v)
{e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;
}
void dfs(int root,int pre)
{sonnum[root]=1;for(int i=head[root];i!=-1;i=e[i].next){int v=e[i].to; if(v==pre) continue;dfs(v,root);sonnum[root]+=sonnum[v];//总个数sonmax[root]=max(sonmax[root],sonnum[v]);//最大子节点
    }
}
int main()
{int T;scanf("%d",&T);while(T--){cnt=0;memset(head,-1,sizeof(head));memset(sonmax,0,sizeof(sonmax));memset(sonnum,0,sizeof(sonnum));scanf("%d",&N);for(int i=1;i<N;i++){int u,v;scanf("%d%d",&u,&v);add_edge(u,v);add_edge(v,u);}dfs(1,0);int mi=INF,pos;for(int i=1;i<=N;i++){int ma=max(sonmax[i],N-sonnum[i]);if(ma<mi){mi=ma;pos=i;}}printf("%d %d\n",pos,mi);}return 0;
}

转载于:https://www.cnblogs.com/caijiaming/p/11552771.html

Balancing Act(树的重心入门)相关推荐

  1. POJ 1655 Balancing Act[树的重心/树形dp]

    Balancing Act 时限:1000ms Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered ...

  2. 『Balancing Act 树的重心』

    树的重心 我们先来认识一下树的重心. 树的重心也叫树的质心.找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 根据树的重心的定义,我们可 ...

  3. POJ-1655 Balancing Act 树的重心

    题意:完全符合树的重心:即找到一个点,其所有的子树中最大的子树节点最少. 代码如下: #include <cstdlib> #include <cstring> #includ ...

  4. POJ 1655 Balancing Act (树的重心)

    题目链接:http://poj.org/problem?id=1655 题意: 求树的重心(最小序号),以及去掉重心后子树节点最大是多少.(树的重心定义为:找到一个点,其所有的子树中最大的子树节点数最 ...

  5. POJ - 1655 Balancing Act(树的重心)

    题目链接:点击查看 题目大意:给一个树,删除其中一个点就会形成一个森林,点的平衡度为删除了这个节点后,所形成多个树,其中组成树的节点最多,节点个数就是那个平衡度. 题目分析:求树的重心,模板题: 树的 ...

  6. poj 1655 Balancing Act 树状dp

    //poj1655树状dp //求一个数的重心 // dfs找到节点v的各个子树的节点数 // 取最大值设为num[v],而v节点以上的子树的节点数 // 为总数-num[v]-1(包括v节点本身)设 ...

  7. 【树形DP】树的重心详解+多组例题详解

    目录 定义: 性质: 算法分析: POJ 1655 Balancing Act(求重心) POJ 3107 Godfather P1364 医院设置(树形DP) 定义: 树的重心也叫树的质心.对于一棵 ...

  8. POJ 1655 Balancing Act (树的重心 + DFS)

    传送门:POJ 1655 题目大意: 求树的重心,如果有多个重心则输出根节点编号最小的一个.树的重心也叫树的质心.找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后 ...

  9. POJ 1655:Balancing Act

    Balancing Act 题目链接: http://poj.org/problem?id=1655 题意: 给出一棵树,求树的重心和以重心为根节点节点最多的子树的节点数,如果有多个重心输出编号较小的 ...

最新文章

  1. 面试官问:能否模拟实现JS的new操作符
  2. 你应该了解的CSS语义化命名方式及常用命名规则
  3. uml图工具_UML建模工具更新情况(二)
  4. 第四节:IO、序列化和反序列化、加密解密技术
  5. 按15分钟取数据_步行15分钟能获得什么?这组数据能告诉你……
  6. P3935 Calculating 整除分块
  7. ASP.NET MVC 的多国语系支持
  8. 计算机组成原理哈工大期末_浅谈计算机组成原理(三)
  9. 微信公众号(考试系统)出现额外的弹框,导致页面关闭,且不保存记录
  10. 对于大家族Sring这些你究竟了解吗
  11. c++获取macos中的uuid的两种方式
  12. 02--Tomcat总体结构分析一
  13. python基础快速入门day01
  14. 拓端tecdat|R语言ARMA GARCH COPULA模型拟合股票收益率时间序列和模拟可视化
  15. vivox50支持鸿蒙,vivo X50系列极致轻薄的机身下,还有哪些功能和亮点?
  16. 产品读书《重新定义公司,谷歌是如何运营的》
  17. Android接入热敏打印机
  18. 带有动态直方图的亲属关系模型
  19. 利用计算机Tracert,计算机网络 Tracert 命令
  20. 转载-高仙机器人落地北京杭州深圳多个城市地铁

热门文章

  1. Jmeter+Ant+Jenkins集成抛出异常java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage
  2. Android 8.0 的部分坑及对应解决方法
  3. 【报告分享】GitHub 2020数字洞察报告.pdf(附下载链接)
  4. 【报告分享】2020技术趋势报告-德勤-202003.pdf(附120页pdf原文下载链接)
  5. 【报告分享】科技抗疫,5G助力:5G通信为公共卫生防控诊疗体系带来的新契机.pdf...
  6. 《统计学习方法》代码全解析——第四部分朴素贝叶斯
  7. 赛道二周冠分享:石头哥有些心里话要说一说
  8. ds1302模块 树莓派_ds1302模块的一个arduino程序
  9. mysql树形结构的效率_MySQL存储树形数据优化技笔记
  10. Facebook广告与Google广告有什么不同?