poj 1655 Balancing Act(树dp)
来源:互联网 发布:网络教育不用考试 编辑:程序博客网 时间:2024/06/13 19:47
Balancing Act
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7986 Accepted: 3290
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
题意:给出一棵树,每个结点都有一个平衡值,一个结点的平衡值是去掉这个结点后形成的森林中最多结点的那棵树的结点数。现在要求出哪个结点的平衡值最小,最小的平衡值为多少。
思路:设以结点i为根的子树的结点数为cnt[i],进行dfs求出每个cnt[i]以及以i为根的树中各个子树的结点数(设最多的结点数为tmp),之后每个结点的平衡值就是max(tmp,n-cnt[i])
AC代码:
#include <iostream>#include <cmath>#include <cstdlib>#include <cstring>#include <cstdio>#include <queue>#include <stack>#include <ctime>#include <map>#include <algorithm>#define ll __int64#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)using namespace std;const int maxn = 20005;vector<int>G[maxn];int n, cnt[maxn], balance[maxn];void dfs(int u, int pre){ cnt[u] = 1; int tmp = 0; for(int i = 0; i < (int)G[u].size(); i++) { int v = G[u][i]; if(v == pre) continue; dfs(v, u); cnt[u] += cnt[v]; tmp = max(tmp, cnt[v]); } balance[u] = max(tmp, n - cnt[u]);}int main(){ int t, a, b; scanf("%d", &t); while(t--) { scanf("%d", &n); for(int i = 0; i <= n; i++) G[i].clear(); for(int i = 0; i < n - 1; i++) { scanf("%d%d", &a, &b); G[a].push_back(b); G[b].push_back(a); } memset(cnt, 0, sizeof(cnt)); dfs(1, -1); int x = 1; for(int i = 1; i <= n; i++) if(balance[i] < balance[x]) x = i; printf("%d %d\n", x, balance[x]); } return 0;}
0 0
- poj 1655 Balancing Act(树dp)
- poj 1655 Balancing Act(树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)
- POJ 1655 Balancing Act(求树的重心--树形DP)
- POJ 1655 Balancing Act (树形dp 树的重心)
- poj-1655-Balancing Act 树形dp/树的重心
- poj 1655 Balancing Act 【树形DP 求树的重心】
- poj 1655 Balancing Act 树状DP,求树的质心
- POJ 1655 Balancing Act 树型DP 树的重心 板题
- poj 1655 Balancing Act(树的重心,树形dp)
- POJ.1655 Balancing Act (树形DP 树的重心)
- (POJ 1655 Balancing Act)树型DP + 删点
- POJ 1655 Balancing Act 树形dp
- AWK案例(1)
- MyEclipse无法编译项目
- CoconutKit:iOS开发必备的开源组件库
- python 实现多个文件拷贝到ssh 目标机子
- JAVA 经典面试题 不要错过哦
- poj 1655 Balancing Act(树dp)
- 详解linux netstat输出的网络连接状态信息
- 简明 Vim 练级攻略
- Android系统架构分析
- Unity中Layer的概念
- CAS单点登录系统整合——注册的问题
- 将SELinux的安全特性加入安卓
- 协议的选择
- c# Socket 异步客户端服务端