(简单) 树形dp POJ 1655 Balancing Act
来源:互联网 发布:手机扒谱软件 编辑:程序博客网 时间:2024/05/21 06:45
Balancing Act
Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 7719Accepted: 3149
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.
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
172 61 21 44 53 73 1
Sample Output
1 2
Source
POJ Monthly--2004.05.15 IOI 2003 sample task
跟 POJ2378一样,我就不多说了
代码:
#include<cstdio>
#include<algorithm>
#include<sstream>
#include<set>
#include<iostream>
#include<map>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 20000+10;
const int inf = 2000000;
#define LL long long
int sum[maxn];
int max_tree[maxn];
int ptr , n , t;
struct Node
{
int v;
Node *next;
}*first[maxn] , edge[maxn*2];
void init()
{
ptr = 0;
memset(first,0,sizeof(first));
}
void add(int x,int y)
{
edge[++ptr].v = y;
edge[ptr].next = first[x];
first[x] = &edge[ptr];
}
void dfs(int x , int fa)
{
Node *p = first[x];
sum[x] = 1;
max_tree[x] = 0;
while (p)
{
int v = p->v;
if (v!=fa)
{
dfs(v,x);
sum[x] += sum[v];
max_tree[x] = max(max_tree[x],sum[v]);
}
p = p->next;
}
}
int main()
{
cin>>t;
while (t--)
{
scanf("%d",&n);
init();
for (int i = 0 ; i < n-1 ; ++i)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
dfs(1,-1);
int balance = inf;
int ret;
for (int i = 1 ; i <= n ; ++i)
{
int tem = max(max_tree[i],n-sum[i]);
if (tem < balance)
{
balance = tem;
ret = i;
}
}
printf("%d %d\n",ret,balance);
}
}
0 0
- POJ--1655--Balancing Act--简单树形DP
- 简单树形dp-poj-1655-Balancing Act
- (简单) 树形dp POJ 1655 Balancing Act
- POJ 1655 Balancing Act 树形dp
- POJ 1655 - Balancing Act 树形DP
- poj 1655 Balancing Act(树形dp)
- poj 1655 Balancing Act(树形DP)
- Balancing Act - POJ 1655 树形dp
- poj 1655 Balancing Act (树形dp)
- POJ 1655 - Balancing Act(树形DP)
- POJ 1655Balancing Act(树形DP)
- poj 1655 Balancing Act(树形DP)
- POJ 1655 Balancing Act 树形dp
- poj 1655 Balancing Act(树形dp)
- Balancing Act+POJ+树形dp
- POJ 1655 Balancing Act(简单树状DP)
- POJ 1655 Balancing Act 树形DP入门题
- poj 1655 Balancing Act(树形DP,删点)
- UBUNTU linux 批量删除文件
- *(中等) 树形dp+分组背包 HDU 4276 The Ghost Blows Light
- (简单) 树形dp HDU 3586 Information Disturbing
- (简单) 树形dp POJ 3107 Godfather
- (简单) 树形dp POJ 2378 Tree Cutting
- (简单) 树形dp POJ 1655 Balancing Act
- 2013年09月24日
- HDU 2110 Crisis of HDU 母函数
- 概率dp POJ 2151 Check the difficulty of problems
- (简单) 树形dp+概率 POJ 3071 Football
- (简单) 状态压缩dp HOJ 2188 WordStack
- (简单) 状态压缩dp HOJ 2665 Factory of XiaoE
- (经典例题) POJ 1185 炮兵阵地
- (简单) 状态压缩dp HOJ 2421 Hie with the Pie
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
体内寒气重
湿重的症状
女人湿热重的症状
体内湿热
体内湿毒重的表现症状
湿重
体内湿热重怎样祛除
痧气
痧气重怎么祛除
体内湿寒重怎样祛除
湿重的人怎样去湿
身体快速祛湿的方法
吃什么可以除湿气
怎样去除体内湿气
如何排除体内湿气
如何去除身体湿气
怎样排除体内湿气
什么菜可以去湿气
怎么去除体内湿气
如何去除体内湿气
什么东西能去湿气
拔火罐可以去湿气吗
如何祛除体内湿气
怎样除体内的湿气
如何清除体内湿气
怎么去身上的湿气
怎样祛除体内湿气
怎么排除体内湿气
如何去除体内湿气寒气
怎样去除体内湿气寒气
什么泡水喝去湿气
怎么去除体内湿气教你6种方法
怎么排出体内湿气
什么水果能去湿气
吃什么东西除湿气
人体为什么有湿气
怎样去身体的湿气
怎样除去体内湿气
怎么消除体内湿气
怎样除去体内的湿气
怎样去除身体里的湿气