POJ 1655 Balancing Act 树形DP入门题
2013-08-29 10:35
267 查看
View Code
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int maxn = 20003; struct node { int v, next, w; }edge[maxn<<1]; int tot; int head[maxn]; int n; void init() { tot = 0; memset(head, -1, sizeof(int) * (n+1)); } void add(int x, int y) { edge[tot].v = y; edge[tot].next = head[x]; head[x] = tot++; } int maxz(int a, int b) { return a > b ? a : b; } int minz(int a, int b) { return a < b ? a : b; } int ans, kk; int num[maxn]; void dfs(int u, int p) { int i, tmp = 0; num[u] = 0; for(i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].v; if(v == p) continue; dfs(v, u); tmp = maxz(tmp, num[v] + 1); num[u] += num[v] + 1; } tmp = maxz(tmp, n - num[u] - 1); if(ans > tmp) { ans = tmp; kk = u; } } int main() { int i, j, cas; int x, y; scanf("%d", &cas); while(cas--) { scanf("%d", &n); init(); for(i = 1; i < n; i++) { scanf("%d%d", &x, &y); add(x, y); add(y, x); } ans = 1<<29; dfs(1, -1); printf("%d %d\n", kk, ans); } return 0; } /* 8 7 2 6 1 2 1 4 4 5 3 7 3 1 */
相关文章推荐
- POJ 1655 Balancing Act 树形DP入门题
- POJ 1655 - Balancing Act 树形DP
- POJ--1655--Balancing Act--简单树形DP
- poj1655 Balancing Act【树的重心+树形dp】
- poj 1655 Balancing Act(树形DP,删点)
- POJ 1655-Balancing Act(树形dp)
- POJ 1655 Balancing Act 树形dp
- POJ 1655 Balancing Act【树形DP】POJ 1655 Balancing Act Balancing Act POJ 1655
- poj 1655Balancing Act(找重心,树形dp)
- poj1655 Balancing Act 【树形DP(很弱)】
- 【POJ - 1655】Balancing Act 【树形DP 求解树的重心】
- poj 1655 Balancing Act(树形dp)
- poj 1655 Balancing Act(树的重心,树形dp)
- Balancing Act - POJ 1655 树形dp
- poj 1655 Balancing Act(树形DP)
- POJ 1655 Balancing Act(树形DP)
- poj 1655 Balancing Act(树形DP)
- POJ 1655 Balancing Act (树形dp 树的重心)
- 【dp每一天】POJ - 1655 Balancing Act(说是树形dp其实就是模拟?)
- poj 1655 Balancing Act 【树形DP 求树的重心】