Balancing Act
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10311   Accepted: 4261

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

表示头一次接触树形dp,一开始的思路想到了要用dfs,并且使用max_i数组表示当前状况下该节点其孩子节点中最大的值,然后sum数组表示当前状况下该节点所带的节点数量,但是这样的话,就得从每一个节点都要深度搜索一遍,才能得到正确结果,结果提交果然TLE。后来发现深度搜索节点度数为1的就够了,结果还是TLE。最后,看到别人的代码,跟我一样的思路,也是sum数组,还有一个son数组,但是用sum[1]-sum[i]表示了除了当前节点的孩子节点,其父亲节点那一个分支段内的节点数量。对这个思路啧啧称奇,责怪自己有没有想到,后面的事情就很简单,之前已经比较过自己的孩子哪一个节点数最多了,这次直接两两比较即可。

代码:

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std; vector <int> node[20005];
int result,result_num;
int used[20005];
int sum[20005];
int max_i[20005]; int dfs(int i)
{
used[i]=1; int k;
sum[i]=0;
max_i[i]=0; for(k=0;k<node[i].size();k++)
{
if(!used[node[i][k]])
{
int temp = dfs(node[i][k]);
sum[i]=sum[i]+temp; if(temp>max_i[i])
{
max_i[i]=temp;
}
}
}
used[i]=0;
return sum[i]+1;
}
int main()
{
int count,N;
cin>>count; while(count--)
{
cin>>N;
int i,flag;
result=20005; memset(node,0,sizeof(node));
memset(used,0,sizeof(used));
for(i=1;i<=N-1;i++)
{
int node1,node2;
cin>>node1>>node2; node[node1].push_back(node2);
node[node2].push_back(node1);
} dfs(1); for(i=1;i<=N;i++)
{
if(max(max_i[i],sum[1]-sum[i])<result)
{
result=max(max_i[i],sum[1]-sum[i]);
result_num=i;
}
} cout<<result_num<<" "<<result<<endl;
} return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. Xcode6新特性(1)-删除Main.storyboard
  2. XAMPP端口占用启动不了
  3. nginx-url重写
  4. 去掉Eclipse中的Validating
  5. hdoj 1395 2^x mod n = 1 【暴力】
  6. 无法打开登录所请求的数据库 &quot;ASPState&quot;。登录失败。 用户 &#39;NT AUTHORITY/SYSTEM&#39; 登录失败。
  7. 调用CMD命令的一个.NET工具类(MyWindowsCmd)
  8. 执行查询&ldquo;BACKUP LOG [XXX] TO DISK = N'F:\\BackData\\事务日至备份\\...&rdquo;失败,错误如下:&ldquo;无法执行 BACKUP LOG,因为当前没有数据库备份。 BACKUP LOG 正在异常终止。
  9. SQL语言的分类
  10. maven项目pom.xml添加main启动类
  11. Java 数据类型与运算符
  12. 转 class和struct最本质的区别
  13. ligerUI利用a标签下载文件
  14. UIAutomator2.0初始
  15. 单点登录(SSO)(原创)
  16. jni 找不到本地方法的实现
  17. 【BZOJ】1857: [Scoi2010]传送带(三分)
  18. cf555e
  19. 配置linux服务器和pycharm的连接
  20. Delphi线程的初级应用

热门文章

  1. Windows Android SDK下载安装,配置,异常问题解决教程
  2. Hot Module Replacement [热模块替换]
  3. Vue源码(上篇)
  4. red hat 7、centos7的root密码破译
  5. 2019上海爱奇艺大数据Java实习生-面试记录
  6. 2016 年 31 款轻量高效的开源 JavaScript 插件和库
  7. springboot,vue,shiro整合 关于登录认证功能
  8. Fescar分布式事务实现原理解析探秘
  9. RedHat OpenShift QuickStart 1.1 OpenShift基础
  10. zabbix开启对中文的支持--&amp;&amp;--中文乱码解决方法