POJ 1655 树的重心 解题报告
来源:互联网 发布:南风知我意txt 编辑:程序博客网 时间:2024/06/07 15:47
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
1
7
2 6
1 2
1 4
4 5
3 7
3 1
Sample Output
1 2
【解题报告】
一不小心做了一道水题
代码如下:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 20010int cnt,head[N];struct Edge{int to,nxt;}e[N<<1];int t,n,dp[N],num[N];void adde(int u,int v){ e[++cnt].to=v;e[cnt].nxt=head[u];head[u]=cnt; e[++cnt].to=u;e[cnt].nxt=head[v];head[v]=cnt;}void dfs(int u,int fa){ dp[u]=0;num[u]=1; for(int i=head[u];~i;i=e[i].nxt) { int v=e[i].to; if(v==fa) 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(){ for(scanf("%d",&t);t;--t) { cnt=-1; memset(head,-1,sizeof(head)); scanf("%d",&n); for(int i=1;i<n;++i) { int u,v;scanf("%d%d",&u,&v); adde(u,v); } 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;}
- POJ 1655 树的重心 解题报告
- POJ 1655 树的重心
- poj 1655 树的重心
- poj 1655 树的重心
- poj-1655 树的重心
- 树的重心 poj-1655
- 树的重心 poj
- POJ 1655 Balancing Act【树的重心】
- POJ 1655(求树的重心)
- poj 1655 Balancing Act 【树的重心】
- POJ-1655 Balancing Act 树的重心
- 【POJ】1655 Balancing Act 树的重心
- 【树的重心】 POJ 1655 Balancing Act
- poj 1655 求树的重心
- POJ 1655 Balancing Act 树的重心
- 【POJ 1655】Balancing Act 【树的重心】
- POJ 1655 Balancing Act 树的重心
- poj 1655 Balancing Act(树的重心)
- [ E10857-01]Oracle® Database Quick Installation Guide 11g Release 2 (11.2) for Linux x86
- 推送
- 友盟QQ整合配置文档
- android 自定义对话框
- Deep Learning论文笔记之(七)深度网络高层特征可视化
- POJ 1655 树的重心 解题报告
- 指定纹理glTexImage2D、GLUtils.texImage2D
- 杨辉三角按行输出--python
- 城市列表配置
- Delphi 调用BarTender打印条码
- c++继承,访问控制
- react-native mac-ios环境搭建
- sqlserver 截取日期年份和月份使用datepart函数
- oracle存储过程的创建及调用案例