POJ 1655 Balancing Act (树的重心)
来源:互联网 发布:调查问卷数据怎么统计 编辑:程序博客网 时间:2024/05/16 01:30
题目地址:POJ 1655
树的重心定义为:找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡.
树的重心可以用树形DP快速的找出来。
代码如下:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>#include <time.h>using namespace std;#define LL __int64#define pi acos(-1.0)//#pragma comment(linker, "/STACK:1024000000")#define root 1, n, 1#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;const int MAXN=20000+10;struct node{ int v, next;}edge[MAXN<<1];int head[MAXN], cnt, ans, min1, n;int sum[MAXN];void add(int u, int v){ edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++;}void init(){ memset(head,-1,sizeof(head)); cnt=0; min1=INF;}void dfs(int u, int fa){ int max1=0, i, tot=0; sum[u]=1; for(i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v; if(v==fa) continue ; dfs(v,u); sum[u]+=sum[v]; max1=max(max1,sum[v]); tot+=sum[v]; } max1=max(max1,n-tot-1); if(min1>max1){ min1=max1; ans=u; }}int main(){ int t, u, v, i; scanf("%d",&t); while(t--){ scanf("%d",&n); init(); for(i=1;i<n;i++){ scanf("%d%d",&u,&v); add(u,v); add(v,u); } dfs(1,-1); printf("%d %d\n",ans,min1); } return 0;}
1 0
- POJ 1655 Balancing Act【树的重心】
- poj 1655 Balancing Act 【树的重心】
- POJ-1655 Balancing Act 树的重心
- 【POJ】1655 Balancing Act 树的重心
- 【树的重心】 POJ 1655 Balancing Act
- POJ 1655 Balancing Act 树的重心
- 【POJ 1655】Balancing Act 【树的重心】
- POJ 1655 Balancing Act 树的重心
- poj 1655 Balancing Act(树的重心)
- POJ 1655 Balancing Act (树的重心)
- poj 1655 Balancing Act 【树的重心】
- 【树的重心】 POJ 1655 Balancing Act
- POJ 1655 Balancing Act 树的重心
- poj 1655 Balancing Act【树的重心】
- poj 1655 Balancing Act(求树的重心)
- poj 1655 Balancing Act(求树的重心)
- 树的重心学习小记 Poj 1655 Balancing Act
- POJ 1655 Balancing Act(求树的重心)
- Using Intel® INDE 2015 to Develop OpenCL™ Applications
- iOS GCD三种创建线程的方式
- 人丑就要多读书(8)
- iOS使用NSOperation创建线程
- 为Android应用增加渠道信息 自动化不同渠道的打包过程
- POJ 1655 Balancing Act (树的重心)
- OpenGL ES2.0---凿岩车钻臂3D案例之MatrixState类详解
- eclipse for JavaEE
- 高德地图SDK使用说明
- maven部署web项目到远程tomcat6
- WPF只能输入数字
- 字符串与日期对象相互转换
- 设置SOCKET的发送与接收缓冲区
- IDL box plot