poj-1655 Balancing Act(树的重心+树形dp)
2016-06-25 13:16
513 查看
题目链接:
[b]Balancing Act[/b]
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
[b]Balancing Act[/b]
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11845 | Accepted: 4993 |
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 题意: 问给的一棵树的重心是哪个节点以及把这个节点去掉后连通块节点个数的最大值; 思路: dfs,找出所有节点的子树节点的个数;再找出去掉这个节点后最大连通块的节点数更新答案就好了; AC代码:
//#include <bits/stdc++.h> #include <vector> #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; #define Riep(n) for(int i=1;i<=n;i++) #define Riop(n) for(int i=0;i<n;i++) #define Rjep(n) for(int j=1;j<=n;j++) #define Rjop(n) for(int j=0;j<n;j++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n'); } const LL mod=1e9+7; const double PI=acos(-1.0); const LL inf=1e18; const int N=2e5+10; const int maxn=1005; int n,son ,ans,num; vector<int>ve ; void dfs(int x,int fa) { int len=ve[x].size(),mmax=0; son[x]=1; for(int i=0;i<len;i++) { int y=ve[x][i]; if(y==fa)continue; dfs(y,x); son[x]+=son[y]; if(son[y]>mmax) mmax=son[y]; } int d=max(mmax,n-son[x]); if(d<=num) { if(d==num) { if(x<ans)ans=x; } else ans=x; num=d; } } int main() { int t; read(t); while(t--) { read(n); for(int i=0;i<=n;i++)ve[i].clear(); int x,y; for(int i=1;i<n;i++) { read(x);read(y); ve[x].push_back(y); ve[y].push_back(x); } ans=1; num=50000000; dfs(1,-1); cout<<ans<<" "<<num<<"\n"; } return 0; }
相关文章推荐
- Struts学习体验【坚持更新】
- Hibernate常用配置文件详解
- 利用session事项用户登录
- CSS权威指南-层叠
- 每天一个Linux命令(13)--less命令
- Linux学习之网络命令
- 使用类图表示你的设计模式——适配器模式!
- Linux系统Centos以及Ubuntu下安装新的python版本
- React Native控件之ScrollView组件详解
- [数据结构]贪婪算法(Dijkstra Algorithm)
- 二维码的扫描与制作
- hdu 2602 (01背包入门)
- java8快速入门
- OpenJudge 8782 乘积最大——S.B.S
- MySQL中多表删除方法(转载)
- PHP数据对象
- <marquee>详细属性
- LeetCode第51题之 N-Queens
- String和StringBuffer区别。
- 如何通过mybatis逆向工程生成实体类以及dao和mapper.xml