poj 1655 Balancing Act 【树形DP 求树的重心】
2015-08-03 10:03
375 查看
Balancing Act
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
Sample Output
1 2
树的重心:对于一棵树上任意的节点i,我们找到它所有子树中节点数最多的子树,若把这棵子树的节点数记为DP[i],那么树的重心就是所有节点中DP值最小的节点。
题意:给你一棵N个点和N-1条边的树,让找出树的重心编号以及重心的DP值,若有相同的DP值,选择编号小的输出。
思路:以节点1为树的根节点,然后遍历所有节点,用树形DP求出所有DP值。最后遍历一下所有节点找重心即可。
求DP值(以1为根节点)
一:下方子树节点数可以在DFS过程中求出;
二:上方子树节点数 = 总节点数N - (下方子树节点总数+1)。
AC代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10726 | Accepted: 4463 |
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
树的重心:对于一棵树上任意的节点i,我们找到它所有子树中节点数最多的子树,若把这棵子树的节点数记为DP[i],那么树的重心就是所有节点中DP值最小的节点。
题意:给你一棵N个点和N-1条边的树,让找出树的重心编号以及重心的DP值,若有相同的DP值,选择编号小的输出。
思路:以节点1为树的根节点,然后遍历所有节点,用树形DP求出所有DP值。最后遍历一下所有节点找重心即可。
求DP值(以1为根节点)
一:下方子树节点数可以在DFS过程中求出;
二:上方子树节点数 = 总节点数N - (下方子树节点总数+1)。
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 20000+100 #define MAXM 40000+1000 using namespace std; struct Edge { int from, to, next; }; Edge edge[MAXM]; int head[MAXN], edgenum; int dp[MAXN];//存储以该节点为根节点的所有子树中 最大的节点数 int num[MAXN];//存储以该节点为根节点的下方树的节点数 int N; void init() { edgenum = 0; memset(head, -1, sizeof(head)); } void addEdge(int u, int v) { Edge E = {u, v, head[u]}; edge[edgenum] = E; head[u] = edgenum++; } void getMap() { int a, b; for(int i = 1; i < N; i++) { scanf("%d%d", &a, &b); addEdge(a, b); addEdge(b, a); } } void dfs(int u, int fa) { num[u] = 1, dp[u] = 0; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(v == fa) continue;//不能再回来找它的父节点 dfs(v, u);//求出以v为根节点的子树的节点数 dp[u] = max(dp[u], num[v]);//更新下方子树 num[u] += num[v];//累加到当前节点 为了求上方子树节点数 } dp[u] = max(dp[u], N - num[u]);//更新上方子树 } void solve() { dfs(1, -1); int ans = N; int node = 1; for(int i = 1; i <= N; i++) { if(ans > dp[i]) ans = dp[i], node = i; } printf("%d %d\n", node, ans); } int main() { int t; scanf("%d", &t); while(t--) { scanf("%d", &N); init(); getMap(); solve(); } return 0; }
相关文章推荐
- 关于github上一个可拓展和折叠的测试demo源码详解
- hdu 1452 happy 2004
- Angular Js ng-repeat指令
- H.264解码器原理
- HTML5与搜索引擎优化[转载]
- 从智能电视开始,PPTV与Letv全线开战?
- uva820 最大流模板题
- 南邮 OJ 1009 2的N次方
- hdoj-1864-最大报销额【动态规划】
- 机电行业如何进行信息化建设
- H.264编码器原理
- H.264量化参数QP和量化步长Qstep
- C#将字节数组加到List中
- xampp终端启动
- yum仅下载RPM包不安装
- adb shell am 的用法
- 随机数相关
- 页面优化的方法有哪些?
- Web框架集合
- iOS 将汉字转换为拼音