Balancing Act
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10311   Accepted: 4261

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

表示头一次接触树形dp,一开始的思路想到了要用dfs,并且使用max_i数组表示当前状况下该节点其孩子节点中最大的值,然后sum数组表示当前状况下该节点所带的节点数量,但是这样的话,就得从每一个节点都要深度搜索一遍,才能得到正确结果,结果提交果然TLE。后来发现深度搜索节点度数为1的就够了,结果还是TLE。最后,看到别人的代码,跟我一样的思路,也是sum数组,还有一个son数组,但是用sum[1]-sum[i]表示了除了当前节点的孩子节点,其父亲节点那一个分支段内的节点数量。对这个思路啧啧称奇,责怪自己有没有想到,后面的事情就很简单,之前已经比较过自己的孩子哪一个节点数最多了,这次直接两两比较即可。

代码:

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std; vector <int> node[20005];
int result,result_num;
int used[20005];
int sum[20005];
int max_i[20005]; int dfs(int i)
{
used[i]=1; int k;
sum[i]=0;
max_i[i]=0; for(k=0;k<node[i].size();k++)
{
if(!used[node[i][k]])
{
int temp = dfs(node[i][k]);
sum[i]=sum[i]+temp; if(temp>max_i[i])
{
max_i[i]=temp;
}
}
}
used[i]=0;
return sum[i]+1;
}
int main()
{
int count,N;
cin>>count; while(count--)
{
cin>>N;
int i,flag;
result=20005; memset(node,0,sizeof(node));
memset(used,0,sizeof(used));
for(i=1;i<=N-1;i++)
{
int node1,node2;
cin>>node1>>node2; node[node1].push_back(node2);
node[node2].push_back(node1);
} dfs(1); for(i=1;i<=N;i++)
{
if(max(max_i[i],sum[1]-sum[i])<result)
{
result=max(max_i[i],sum[1]-sum[i]);
result_num=i;
}
} cout<<result_num<<" "<<result<<endl;
} return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1655:Balancing Act的更多相关文章

  1. 【POJ 1655】 Balancing Act

    [题目链接] 点击打开链接 [算法] 树形DP求树的重心 [代码] #include <algorithm> #include <bitset> #include <cc ...

  2. poj-1655 Balancing Act(树的重心+树形dp)

    题目链接: Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11845   Accepted: 4 ...

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

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

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

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

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

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

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

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

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

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

  8. POJ 1655 Balancing Act 树的重心

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

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

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

随机推荐

  1. MyuCMS_V2.1漏洞分析

    前言 在CNVD看到一个MyuCMS的一个任意文件删除漏洞.然后去搜了下这个CMS,发现官网公告显示在V2.2.3版本修复了CNVD提供的多处漏洞. 怀着好奇的心里,去CNVD搜了下这个CMS,结果发 ...

  2. SpringBoot图文教程3—「‘初恋’情结」集成Jsp

    有天上飞的概念,就要有落地的实现 概念+代码实现是本文的特点,教程将涵盖完整的图文教程,代码案例 文章结尾配套自测面试题,学完技术自我测试更扎实 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例 ...

  3. 零基础学完Python的7大就业方向,哪个赚钱多?

    “ 我想学 Python,但是学完 Python 后都能干啥 ?” “ 现在学 Python,哪个方向最简单?哪个方向最吃香 ?” “ …… ” 相信不少 Python 的初学者,都会遇到上面的这些问 ...

  4. Flask - 请求扩展,钩子函数(Django的中间件) --> 请求前,中,后,

    例子1. 处理请求之前 @app.before_request 在请求之前,这个被装饰的函数会被执行 用户登录验证代码可以在这里写 @app.before_request def process_re ...

  5. [Linux] day05——命令行

    --------------------linux命令 实现某一功能指令或程序 命令行执行依赖于解释器linux命令的分类 内部命令 属于shell解释器一部分 /bin/bash 外部命令 独立与s ...

  6. 吴裕雄--天生自然HADOOP操作实验学习笔记:qq好友推荐算法

    实验目的 初步认识图计算的知识点 复习mapreduce的知识点,复习自定义排序分组的方法 学会设计mapreduce程序解决实际问题 实验原理 QQ好友推荐算法是所有推荐算法中思路最简单的,我们利用 ...

  7. natapp 内网穿透服务

    参考文章:https://www.jianshu.com/p/91a321e584b8 参考文章:https://www.jianshu.com/p/c4cb8666c96a 一.什么是内网穿透 通过 ...

  8. c++ (翁恺浙大公开课)前言、目录

    c++语言比较复杂,学习起来相对难一些,加之特性繁多,很难全部掌握:特别是工作几年之后,每次温故都有很大的收获,之前不懂的地方随着工作的积累和重新的学习,都会慢慢的解开,当然我现在还是很菜... 之所 ...

  9. 「NOI2009」二叉查找树

    传送门 Luogu 解题思路 看一眼题面,显然这是一颗 treap ,考虑到这棵 treap 的中序遍历总是不变的,所以我们就先把所有点按照数据值排序,求出 treap 的中序遍历,然后还可以观察到, ...

  10. 090、Java中String类之判断两个int型整数是否相等

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...