Poj 3107 Godfather (树的重心)
来源:互联网 发布:手机qq网络硬盘 编辑:程序博客网 时间:2024/05/16 13:04
题意:给定一棵树,求树的所有重心,按照编号从小到大的顺序输出.
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define max(a,b) ((a)>(b)?(a):(b))#define min(a,b) ((a)<(b)?(a):(b))const int N=50005;struct Edge{int from,to,next;}edge[N<<1];int head[N],e,dp[N]; //所有子树中节点数的最大值,不含自身int n,son[N]; //以每个节点为根的子树的节点数,包含自身int ans[N];void Add (int u,int v){Edge E = {u, v, head[u]};edge[e] = E;head[u] = e++;}void DFS (int u, int pre){son[u]=1;dp[u]=0;for (int i=head[u]; ~i; i=edge[i].next){int v=edge[i].to;if (v==pre) continue;DFS(v,u);son[u]+=son[v];dp[u]=max(dp[u],son[v]);}dp[u]=max(dp[u],n-son[u]);}int main (){while (~scanf("%d",&n)){memset(head,-1,sizeof(head));e=0;int i;for (i=1;i<n;i++){int u,v;scanf("%d%d",&u,&v);Add(u,v);Add(v,u);}DFS(1,-1);int minnum=N,cnt=0;for (i=1;i<=n;i++)if (minnum > dp[i]){cnt=0;minnum=dp[i];ans[cnt++]=i;}else if (minnum==dp[i])ans[cnt++]=i;for (i=0;i<cnt;i++)printf(i==cnt-1?"%d\n":"%d ",ans[i]);}return 0;}
0 0
- Poj 3107 Godfather (树的重心)
- POJ 3107 Godfather (树的重心)
- poj 3107 Godfather 求树的重心
- poj 3107 Godfather(树的重心)
- 【POJ 3107 】Godfather 【树的重心】
- poj 3107 Godfather(树形dp,树的重心)
- POJ 题目3107 Godfather(树的重心)
- POJ 3107 Godfather (求树的重心)
- poj 3107 Godfather(树的重心问题)
- POJ.3107 Godfather (树形DP 树的重心)
- POJ 3107 Godfather (树的重心模板题)
- Poj 2599 Godfather(树的重心)
- POJ 3107 Godfather 求重心
- POJ-3107 Godfather & POJ-2378 Tree Cutting(树的重心)
- poj 3107 Godfather 求树的所有重心 树形DP基础题
- POJ3107 Godfather 求树的重心
- POJ3107-树的重心&树形DP-Godfather
- poj3107 Godfather(树的重心)
- Android自定义控件实战——下拉刷新控件终结者:PullToRefreshLayout
- hive的join和复合数据类型
- 如何利用android来赚钱
- 使用next_permutation函数生成全排列
- Linux下的压缩解压缩命令详解
- Poj 3107 Godfather (树的重心)
- Spring4的学习(四)
- logback日志使用记录
- 总结一些常用的第三方库(iOS)
- Runtime Error(ACCESS_VIOLATION)
- android面试题总结加强版之三
- java中entry遍历map
- 6.jQuery DOM 节点操作
- [leetcode] Trapping Rain Water