POJ 1655 Balancing Act (求树的重心)
2013-11-16 22:34
495 查看
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
Source
POJ Monthly--2004.05.15 IOI 2003 sample task
简单的树形DP。
水题,为了搞树的分治,先来A掉这个题目。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7859 | Accepted: 3210 |
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
Source
POJ Monthly--2004.05.15 IOI 2003 sample task
简单的树形DP。
水题,为了搞树的分治,先来A掉这个题目。
/* *********************************************** Author :kuangbin Created Time :2013-11-16 22:17:48 File Name :E:\2013ACM\专题学习\树的分治\POJ1655.cpp ************************************************ */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; const int MAXN = 20010; struct Edge { int to,next; }edge[MAXN*2]; int head[MAXN],tot; void init() { memset(head,-1,sizeof(head)); tot = 0; } void addedge(int u,int v) { edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; } int dp[MAXN],num[MAXN]; int n; void dfs(int u,int pre) { dp[u] = 0;num[u] = 1; for(int i = head[u];i != -1;i = edge[i].next) { int v = edge[i].to; if(v == pre)continue; dfs(v,u); dp[u] = max(dp[u],num[v]); num[u] += num[v]; } dp[u] = max(dp[u],n - num[u]); } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T; scanf("%d",&T); int u,v; while(T--) { scanf("%d",&n); init(); for(int i = 1;i < n;i++) { scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } dfs(1,-1); int ans1 = 1, ans2 = dp[1]; for(int i = 2;i <= n;i++) if(ans2 > dp[i]) { ans1 = i; ans2 = dp[i]; } printf("%d %d\n",ans1,ans2); } return 0; }
相关文章推荐
- POJ1655 Balancing Act——树的重心,Dfs
- POJ1655 Balancing Act (树的重心)
- poj1655 Balancing Act 找树的重心
- 【POJ - 1655】Balancing Act 【树形DP 求解树的重心】
- POJ 1655 Balancing Act(求树的重心)
- POJ-1655 Balancing Act(树的重心)
- POJ 1655 Balancing Act (树的重心 + DFS)
- poj 1655 Balancing Act 求树的重心
- 【树的重心】 POJ 1655 Balancing Act
- poj1655 Balancing Act【树的重心+树形dp】
- POJ 1655 Balancing Act (树的重心)
- POJ-1655 Balancing Act 树的重心
- poj 1655Balancing Act(找重心,树形dp)
- POJ 1655 Balancing Act (树的重心)
- POJ 1655 Balancing Act( 树的重心 )
- poj-1655 Balancing Act(树的重心+树形dp)
- POJ 1655 Balancing Act (树的重心,常规)
- poj 1655 Balancing Act 求树的重心
- POJ 1655 Balancing Act【树的重心】
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)