Balancing Act
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13865   Accepted: 5880

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

 
【题解】
此题用所谓“DP”?求重心即可,更新时时刻注意更新最小节点
一直不理解为啥求重心也叫DP啊!
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib> inline void read(int &x)
{
x = ;char ch = getchar();char c = ch;
while(ch > '' || ch < '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
}
inline int max(int a, int b){return a > b ? a : b;}
inline int min(int a, int b){return a < b ? a : b;} const int INF = 0x3f3f3f3f;
const int MAXN = + ; struct Edge
{
int u,v,next;
}edge[MAXN << ];
int t,n,head[MAXN],cnt,b[MAXN],dp[MAXN],ma,g;
inline void insert(int a, int b){edge[++cnt] = Edge{a,b,head[a]};head[a] = cnt;} void dfs(int u)
{
int pos, tmp = -;
dp[u] = ;
for(pos = head[u];pos;pos = edge[pos].next)
{
int v = edge[pos].v;
if(!b[v])
{
b[v] = true;
dfs(v);
dp[u] += dp[v];
tmp = max(tmp, dp[v]);
}
}
tmp = max(tmp, n - dp[u]);
if(tmp < ma)
ma = tmp,g = u;
if(tmp == ma)
g = min(g, u);
} int main()
{
read(t);
register int i,tmp1,tmp2;
for(;t;--t)
{
ma = INF,g = INF;
memset(edge, , sizeof(edge));
memset(head, , sizeof(head));
cnt = ;memset(dp, , sizeof(dp));
memset(b, , sizeof(b));
read(n);
for(i = ;i < n;++ i)
{
read(tmp1);read(tmp2);
insert(tmp1, tmp2);insert(tmp2, tmp1);
}
b[] = true;
dfs();
printf("%d %d\n", g, ma);
}
return ;
}

最新文章

  1. SQL Server-聚焦使用视图若干限制/建议、视图查询性能问题,你懵逼了?(二十五)
  2. 基数树与RCU锁
  3. [outlook]打开以后就自动进入安全模式的解决方法。Outlook start in safe mode.
  4. [转发]dsdt解决睡眠唤醒死机
  5. Hibernate详解(5)——Hibernate核心接口和工作原理
  6. cobol语言基础培训教程
  7. JavaScript window.setTimeout() 的详细用法
  8. day03 文件操作
  9. semantic-ui 输入框
  10. 将 Windows 虚拟机从非托管磁盘转换为托管磁盘
  11. 373. Find K Pairs with Smallest Sums (java,优先队列)
  12. Android 程序打包及签名(转)
  13. CSS3组件化之菊花loading
  14. 学习 C++,关键是要理解概念,而不应过于深究语言的技术细节
  15. html5 data属性的使用
  16. spring 包的依赖关系
  17. 2017-2018-1 20179202《Linux内核原理与分析》第六周作业
  18. Jenkins Slave Nodes – using the Swarm Plugin
  19. flash读写学习笔记与spi接口及简单测试验证(三)
  20. Java for LeetCode 086

热门文章

  1. for update行级锁的作用
  2. 新增的Java MapReduce API
  3. Luogu P3496 [POI2010]GIL-Guilds(贪心+搜索)
  4. odoo many2one
  5. Vue简单评星效果与单张图片上传
  6. 深入浅出 Java Concurrency (18): 并发容器 part 3 ConcurrentMap (3)[转]
  7. Django REST Framework概述
  8. finger 工具:用来查询用户信息,侧重用户家目录、登录SHELL等
  9. Leetcode504.Base 7七进制数
  10. TCP/TP:DNS区域(Zone)