poj 3107 Godfather(树形dp)
2017-08-28 19:58
337 查看
树的重心。
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int MAXN = 50010; const int MAXM = 100010; struct Edge { int to,next; }edge[MAXM]; int head[MAXN]; int son[MAXN]; int MAX[MAXN]; int res,n,tot; void init() { res = MAXN; tot = 0; memset(head,-1,sizeof(head)); memset(son,0,sizeof(son)); memset(MAX,0,sizeof(MAX)); } void addedge(int u, int v) { edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; } void dfs(int u, int fa) { son[u] = 1; int ans = 0; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(v == fa) continue; dfs(v,u); son[u] += son[v]; ans = max(ans,son[v]); } ans = max(ans,n-son[u]); if(ans < res) res = ans; MAX[u] = ans; } int main() { init(); int a,b; scanf("%d",&n); for(int i = 0; i < n-1; ++i) { scanf("%d %d",&a,&b); addedge(a,b); addedge(b,a); } dfs(1,-1); for(int i = 1; i <= n; ++i) { if(MAX[i] == res) printf("%d ",i); } printf("\n"); return 0; }
相关文章推荐
- POJ 3107-Godfather(树形dp)
- POJ 3107 Godfather(树形DP)
- POJ 3107 Godfather 【树形DP】
- poj 3107 Godfather(树形DP,点的个数较多, 删点使得剩余部分结点最多的最小值)
- poj 3107 Godfather(树形DP)
- POJ 3107 - Godfather 树形DP..vector慎用...
- poj 3107 Godfather 求树的所有重心 树形DP
- poj 3107 Godfather(树形dp)
- 【dp每一天】POJ - 3107 Godfather (树形dp)
- POJ 3107 Godfather (树形dp)
- poj 3107 Godfather(树形dp)
- Godfather - POJ 3107 树形dp
- POJ 3107 Godfather (水题,树形DP)
- POJ 3107 Godfather(树形dp)
- POJ 3107 Godfather(树形DP)
- 【POJ】3107 Godfather 树形dp
- poj 3107 Godfather(树形dp,树的重心)
- poj 3107 Godfather (树形dp)
- POJ 3107 Godfather (树形DP)
- poj 3107 Godfather (树形dfs)