博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1655 Balancing Act(树的重心)
阅读量:6712 次
发布时间:2019-06-25

本文共 2284 字,大约阅读时间需要 7 分钟。

Balancing Act
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14062   Accepted: 5937

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

172 61 21 44 53 73 1

Sample Output

1 2

Source

 
dp求树的重心:
我们首先找到每一个节点所有子树的大小。
然后用n-size[pos]算出祖先的大小,
判断即可
 
1 #include
2 #include
3 #include
4 #include
5 #define lli long long int 6 using namespace std; 7 const int MAXN=2000001; 8 const int maxn=0x7fffff; 9 void read(int &n)10 {11 char c='+';int x=0;bool flag=0;12 while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}13 while(c>='0'&&c<='9')14 x=(x<<1)+(x<<3)+c-48,c=getchar();15 flag==1?n=-x:n=x;16 }17 struct node18 {19 int u,v,w,nxt;20 }edge[MAXN];21 int head[MAXN];22 int num=1;23 int size[MAXN];24 int ans=maxn;25 int out=maxn;26 int n;27 int mx[MAXN];28 void add_edge(int x,int y)29 {30 edge[num].u=x;31 edge[num].v=y;32 edge[num].nxt=head[x];33 head[x]=num++;34 }35 void dfs(int pos,int fa)36 {37 // cout<
<

 

转载地址:http://xrhlo.baihongyu.com/

你可能感兴趣的文章
Python yield与实现
查看>>
购物车特效收集
查看>>
Access中一句查询代码实现Excel数据导入导出
查看>>
2015第49周二
查看>>
Sphinx/Coreseek 4.1的安装流程
查看>>
邮件服务器Postfix的管理 重启php-fpm
查看>>
Android Studio 项目代码全部消失--出现原因及解决方法
查看>>
SQL Server---存储过程
查看>>
MySQL Performance-Schema(二) 理论篇
查看>>
搭建SSH详细步骤及相关说明
查看>>
Android IOS WebRTC 音视频开发总结(五五)-- 音视频通讯中的抗丢包与带宽自适应原理...
查看>>
Libgdx: 将Texturepacker打包的PNG图片还原成一张一张的单个的
查看>>
再议Swift操作符重载
查看>>
pc机进入android的shell
查看>>
javascript Date format(js日期格式化)
查看>>
Loadrunner中参数化实战(6)-Random+Each occurrence
查看>>
tomcatserver解析(六)-- Acceptor
查看>>
asp.net判断访问者是否来自移动端
查看>>
Python 一些常用模块的安装
查看>>
严苛模式(StrictMode)
查看>>