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. [转]符号和运算符参考 (F#)

    本文转自:http://msdn.microsoft.com/zh-cn/library/dd233228.aspx 本主题包含一个表,其中列出了 F# 语言中使用的符号和运算符. 符号和运算符表   ...

  2. Sql Server把本地数据库传到服务器数据库

    上一篇文章我们已经把网站布署到服务器中了,如果是动态网站肯定是有数据库的,接下来通过Sql Server把本地数据库上传到服务器数据库中. 打开Sql Server连接本地数据库,选中要导出的数据库, ...

  3. python版本2和3使用range()函数方法

    python 2:可以直接使用range(5) 输入的列表结果和预期的一样 python 3:使用range(5) 得到列表结果却是这个,和预期的不一致,其原因是节省空间,防止过大的列表产生 如果想要 ...

  4. 如何在tomcat部署项目(用ip访问)

    找了好长时间的错误,server.xml中一点错误也没有,但就是访问不到,最终发现就是服务器没有开放80端口的缘故. 服务器是Windows系统 1.控制面板=>系统和安全=>Window ...

  5. CF916C Jamie and Interesting Graph

    思路:构造 实现: #include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n ...

  6. input 全选 jquery封装方法

    HTML代码 <table class="table table-striped"> <thead> <tr> <th><in ...

  7. javascript中函数的四种调用模式详解

    介绍函数四种调用模式前,我们先来了解一下函数和方法的概念,其实函数和方法本质是一样,就是称呼不一样而已.函数:如果一个函数与任何对象关系,就称该函数为函数.方法:如果一个函数作为一个对象属性存在,我们 ...

  8. 搜索可用docker镜像

    简介:这一步的目标是学会使用docker search命令来检索可用镜像. 搜索可用的docker镜像 目标: 提示: 正确的命令: 搜索可用的docker镜像 使用docker最简单的方式莫过于从现 ...

  9. Python游戏开发入门

    Pygame简介与安装 1.Pygame安装 pip install pygame2.检测pygame是否安装成功 python -m pygame.examples.aliens Pygame最小开 ...

  10. PC端浏览器定位

    第一: PC端浏览器定位(纯前端)浏览器定位 :这里用了两种 ,一种是Html5自带的方法 另一种是引用了百度api  ,百度api 的使用有三种:1 浏览器定位2 ip定位3 SDK辅助定位引用百度 ...