Balancing Act
 

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. 1
  2. 7
  3. 2 6
  4. 1 2
  5. 1 4
  6. 4 5
  7. 3 7
  8. 3 1

Sample Output

  1. 1 2

题意:

  给你n点的树

  让你删去一个点,剩下的 子树中节点数最多的就是删除这个点的 价值

  求删除哪个点 价值最小就是重心 

题解:

  来来来

  认识一下什么重心

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <vector>
  6. #include <algorithm>
  7. using namespace std;
  8. const int N = 1e5+, M = 1e2+, mod = 1e9+, inf = 1e9+;
  9. typedef long long ll;
  10.  
  11. vector<int > G[N];
  12. int T,n,siz[N],mx,mx1;
  13. void dfs(int u,int fa) {
  14. siz[u] = ;
  15. int ret = ;
  16. for(int i=;i<G[u].size();i++) {
  17. int to = G[u][i];
  18. if(to == fa) continue;
  19. dfs(to,u);
  20. siz[u] += siz[to];
  21. ret = max(ret , siz[to]);
  22. }
  23. if(u!=) ret = max(ret , n - siz[u]);
  24. if(ret <= mx) {
  25. mx1 = u;
  26. mx = ret;
  27. }
  28. }
  29. int main()
  30. {
  31. scanf("%d",&T);
  32. while(T--) {
  33. scanf("%d",&n);
  34. for(int i=;i<N;i++) G[i].clear();
  35. for(int i=;i<n;i++) {
  36. int a,b;
  37. scanf("%d%d",&a,&b);
  38. G[a].push_back(b);
  39. G[b].push_back(a);
  40. }
  41. mx = inf;
  42. dfs(,-);
  43. printf("%d %d\n",mx1 , mx);
  44. }
  45. }

POJ 1655 Balancing Act 树的重心的更多相关文章

  1. POJ 1655 - Balancing Act 树型DP

    这题和POJ 3107 - Godfather异曲同工...http://blog.csdn.net/kk303/article/details/9387251 Program: #include&l ...

  2. POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)

    关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...

  3. poj 1655 Balancing Act 求树的重心【树形dp】

    poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...

  4. POJ 1655 Balancing Act【树的重心】

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14251   Accepted: 6027 De ...

  5. POJ 1655.Balancing Act 树形dp 树的重心

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14550   Accepted: 6173 De ...

  6. poj 1655 Balancing Act(找树的重心)

    Balancing Act POJ - 1655 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. /* 找树的重心可以用树形dp或 ...

  7. POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)

    树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...

  8. POJ 1655 - Balancing Act - [DFS][树的重心]

    链接:http://poj.org/problem?id=1655 Time Limit: 1000MS Memory Limit: 65536K Description Consider a tre ...

  9. POJ 1655 Balancing Act【树的重心模板题】

    传送门:http://poj.org/problem?id=1655 题意:有T组数据,求出每组数据所构成的树的重心,输出这个树的重心的编号,并且输出重心删除后得到的最大子树的节点个数,如果个数相同, ...

随机推荐

  1. ACM/ICPC 之 枚举(POJ1681-画家问题+POJ1166-拨钟问题+POJ1054-讨厌的青蛙)

    POJ1681-画家问题 枚举的经典例题,枚举第一行即可,其余行唯一. //画家问题,y表示黄色,w表示白色,怎样让墙上所有方格为y,操作类似熄灯问题poj1222 //memory 136K Tim ...

  2. [第三方]SDWebImage获取网络图片控件的用法

    #import "UIImageView+WebCache.h" @interface WeatherViewController ()<UISearchBarDelegat ...

  3. 【leetcode】Binary Tree Maximum Path Sum (medium)

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  4. POJ 1830 开关问题 (高斯消元)

    题目链接 题意:中文题,和上篇博客POJ 1222是一类题. 题解:如果有解,解的个数便是2^(自由变元个数),因为每个变元都有两种选择. 代码: #include <iostream> ...

  5. 【XLL API 函数】xlfUnregister (Form 1)

    此函数可以被 Excel 已经载入的 XLL 或 DLL 调用.它等效于宏表函数 UNREGISTER. xlfUnregister 有两种调用形式: 形式1:Unregister 单独的命令或函数 ...

  6. (转)Delphi工程文件说明

    1.DPR: Delphi Project文件,包含了Pascal代码.应用系统的工程文件2.PAS: Pascal文件,Pascal单元的源代码,可以是与窗体有关的单元或是独立的单元.3.DFM:D ...

  7. qt_计算器的简单实现

    //阶乘不知道怎么实现不了/(ㄒoㄒ)/~~,以后慢慢调试吧......... //转换为后缀表达式,实现最主要功能 void MainWindow::toPostfix () { QString e ...

  8. http协议之request

    一.请求的基本格式 请求的基本格式包括请求行,请求头,请求实体三部分.例如:GET /img/bd_logo1.png HTTP/1.1Accept: */*Referer: http://www.b ...

  9. 浅析 - Storyboard / Xib

    大家都知道纯代码写应用的成本是很高的,特别是涉及到UI界面的实现,相当耗费时间.之前自己写应用时有了解过Storyboard,也简单使用过,但随着最近深入了解它之后,发现自己低估了它的作用和影响力,因 ...

  10. Java -- File

    @.getPath().getAbsolutePath().getCanonicalPath()区别 原文:http://blog.csdn.net/wh_19910525/article/detai ...