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,注意考虑n-sum[u]这个搜索方向的联通点集
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 200099
#define L 31
#define INF 1000000009
#define eps 0.00000001
#define sf(a) scanf("%d",&a)
/*
dp[i] 记录i点除去偶的最大点集点数
sum[i] 记录所有子节点数目
*/
struct edge
{
int to, next;
}E[MAXN];
int sum[MAXN], dp[MAXN], head[MAXN];
int t, n, cnt;
void addedge(int f,int t)
{
E[cnt].to = t;
E[cnt].next = head[f];
head[f] = cnt++;
}
void init()
{
memset(sum, , sizeof(sum));
memset(dp, , sizeof(dp));
memset(head, -, sizeof(head));
cnt = ;
}
void dfs(int u, int pre)
{
sum[u] = dp[u] = ;
for (int i = head[u]; i != -; i = E[i].next)
{
int v = E[i].to;
if (v == pre) continue;
dfs(v, u);
sum[u] += sum[v];
dp[u] = max(dp[u], sum[v]);
}
dp[u] = max(dp[u], n - sum[u]);
}
int main()
{
sf(t);
while (t--)
{
init();
sf(n);
for (int i = ; i < n - ; i++)
{
int a, b;
sf(a), sf(b);
addedge(a, b);
addedge(b, a);
}
dfs(, -);
int ans = INF, k = -;
for (int i = ; i <= n; i++)
{
if (dp[i] < ans)
{
k = i, ans = dp[i];
}
}
printf("%d %d\n", k, ans);
}
}

I - Balancing Act POJ - 1655的更多相关文章

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

    Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the t ...

  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&&POJ 3107 Godfather(树的重心)

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

  4. POJ 1655 Balancing Act && POJ 3107 Godfather

    题目大意: 根据题目的图很好理解意思,就是记录每一个点的balance,例如 i 的balance就是把 i 从这棵树中除去后得到的森林中含有结点数最多 的子树中的节点个数,然后找到所有节点中对应的b ...

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

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

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

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

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

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

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

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

  9. POJ 1655 Balancing Act 树的重心

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

随机推荐

  1. POI导出时,将指定的列设置为下拉列表

    本示例设置第2列为下拉框(下拉框内容为:是/否),从第5行开始到5657行结束. 关键代码示例: ComboxList = new String[]{"是","否&quo ...

  2. Node.js——require加载规则

    判断require中的标识参数: 非路径的标识参数:也被称为是核心模块,已经被编译到二进制文件中 带有路径标识参数:自定义模块,一般都是相对定位 第三方模块:表现形式与核心模块一样,但是实际不一样,它 ...

  3. 【经验分享】IMX6开发板编译问题及解决方法

    本文转自迅为IMX6开发板售后讨论群,分享给大家~物理主机 win10 64 位专业版.虚拟机 VM12 Pro.开发环境采用迅为提供的开发环境:Ubuntu12.04.2 .镜像采用最新的:iTOP ...

  4. javascript的prototype经典使用场景

    prototype的经典使用场景就是为对象增加属性和方法,如给自定义的Man对象增加个姓名属性和语言方法: function man() {        this.age = "22&qu ...

  5. Android图像处理之Bitmap类(1)

    Bitmap是Android系统中的图像处理的最重要类之一.用它可以获取图像文件信息,进行图像剪切.旋转.缩放等操作,并可以指定格式保存图像文件.本文从应用的角度,着重介绍怎么用Bitmap来实现这些 ...

  6. zay大爷的神仙题目 D1T3-膜你抄

    依旧是外链 锦鲤抄 [题目背景] 你在尘世中辗转了千百年 却只让我看你最后一眼 火光描摹容颜燃尽了时间 别留我一人,孑然一身 凋零在梦境里面. ——银临&云の泣<锦鲤抄> [问题描 ...

  7. Android中notifyDataSetInvalidated()和notifyDataSetChanged()有什么区别

     看下源码中对于这两个方法   public void notifyDataSetChanged () 该方法内部实现了在每个观察者上面调用onChanged事件.每当发现数据集有改变的情况,或者读取 ...

  8. C++ 指针形参和指针引用形参的原理分析

    C++ 函数的参数传递可以分为:值传递和引用传递. 两者的最大区别也很简单,如果该函数的参数只是读的话,值传递就可以满足.如果该函数的参数需要进行修改并返回的时候,就应该进行引用传递. C++指针作为 ...

  9. Python学习-range的用法

    range() 函数的用法 range(start,end,step):可以参见已连串的数字,常与for循环配合使用 参数详解如下 start:开始创建的起始位置,默认为0 end:开始创建的结束位置 ...

  10. Mysql 字符函数详解

    MySql 所有字符串函数函数详解 ASCII(str) 返回str最左边第一位字符的ASCII编码,如果str为空,则返回 0 .如果str为NULL,则返回NULL -- 只返回a的ASCII编码 ...