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. highcharts Ajax 动态请求加载数据

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  2. ORACLE varchar2类型的字段更改为clob

    将varchar2类型字段改成clob类型    --增加临时新字段 alter table base_temp add temp clob; --将需要改成大字段的项内容copy到大字段中updat ...

  3. Systemverilog for design 笔记(一)

    转载请标明出处 一.     System Verilog 声明的位置 1.       包(packages) Verilog要求局部声明: variables, nets, tasks and f ...

  4. 0-1背包问题(0-1 knapsack problem)

    0-1背包问题描述:一个正在抢劫商店的小偷发现了n个商品,第i个商品价值 vi 美元,重 wi 磅,vi 和 wi 都是整数.这个小偷希望拿走价值尽量高的商品,但他的背包最多能容纳 S 磅重的商品,S ...

  5. 腾讯云服务器安装python3

    一,开始安装python3    首先安装相关包,这里千万不能忽视,不然有什么不可预见的错误会很难受.其命令如下: yum install zlib-devel bzip2-devel openssl ...

  6. CentOS 下的apache服务器配置与管理

    一.WEB服务器与Apache1.web服务器与网址 2.Apache的历史 3.补充http://www.netcraft.com/可以查看apache服务器的市场占有率同时必须注意的是ngnix, ...

  7. python下载图片的代码块

    import urllib.requestimgurl="https://ss3.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/si ...

  8. SQL注入之SQLmap

    注意:sqlmap只是用来检测和利用sql注入点的,并不能扫描出网站有哪些漏洞,使用前请先使用扫描工具扫出sql注入点. 1.

  9. 【剑指Offer面试编程题】题目1523:从上往下打印二叉树--九度OJ

    题目描述: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 输入: 输入可能包含多个测试样例,输入以EOF结束. 对于每个测试案例,输入的第一行一个整数n(1<=n<=1000, ...

  10. 2_02_MSSQL课程_where查询和like模糊查询

    1.where 条件过滤 常见的表达式过滤:比如: select * from 表 where Id>10; 多条件过滤: and or not    (优先级:not > and > ...