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. android开发学习——Error:Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/zxing/BarcodeFormat;

    在Android Studio中,sync project没有错,但是run时会报错; http://blog.csdn.net/q568430333/article/details/50969033 ...

  2. 平板&Safari 开发tips

    css: *{ margin: 0; padding: 0;  /* 禁止用户点选网页内容 */ -webkit-touch-callout:none; -webkit-user-select:non ...

  3. 一个简单的Java代码生成工具—根据数据源自动生成bean、dao、mapper.xml、service、serviceImpl

    目录结构 核心思想 通过properties文件获取数据源—>获取数据表的字段名称.字段类型等—>生成相应的bean实体类(po.model).dao接口(基本的增删改查).mapper. ...

  4. 《Hadoop高级编程》之为Hadoop实现构建企业级安全解决方案

    本章内容提要 ●    理解企业级应用的安全顾虑 ●    理解Hadoop尚未为企业级应用提供的安全机制 ●    考察用于构建企业级安全解决方案的方法 第10章讨论了Hadoop安全性以及Hado ...

  5. 键盘工具栏的快速集成--IQKeyboardManager

    转自:http://www.cnblogs.com/gaoxiaoniu/p/5333187.html 键盘工具栏的快速集成--IQKeyboardManager IQKeyboardManager, ...

  6. Winform中ListView鼠标移动使用toolTip显示信息

    今天在做一个酒店管理系统的时候用到了ListView,突然想到是否能够当鼠标移动到某一项的时候给出具体房间的信息呢! 首先设置Listview的MouseMove事件 1.获取当前坐标的项 ListV ...

  7. Windows之shortcut

    System key combinations CTRL+ESC: Open Start menu ALT+TAB: Switch between open programs ALT+F4: Quit ...

  8. python练习2 购物车程序

    # -*- coding: utf-8 -*-# @Time : 2018/10/18 16:06# @Author : Two Brother# @EMAIL : yeluyide@163.com# ...

  9. vue中滚动页面,改变样式&&导航栏滚动时,样式透明度修改

    vue中滚动页面,改变样式&&导航栏滚动时,样式透明度修改.vue <div class="commonHeader" v-bind:class=" ...

  10. 11-2 numpy/pandas/matplotlib模块

    目录 numpy模块 一维数组 二维数组 列表list和numpy的区别 获取多维数组的行和列 多维数组的索引 高级功能 多维数组的合并 通过函数方法创建多维数组 矩阵的运算 求最大值最小值 nump ...