poj 1655 树形dp
来源:互联网 发布:sql select 结果 左链 编辑:程序博客网 时间:2024/05/17 08:20
题意相当于给你一棵树 让你求每个点的不同子树上的节点个数吧 注意存边的时候存双向边
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;struct node{int to,next;}A[40010];int tot,list[20010],mark[20010],n;int add(int a,int b){A[++tot].to = b; A[tot].next = list[a]; list[a] = tot; return 0; }int dp[20010],subtree[20010];int max(int a,int b){return a>b?a:b;}int dfs(int point){mark[point]=1;int b;int k=list[point];if(k==-1) {dp[point]=n-1;subtree[point]=0;return 0;}for(;k!=-1;k = A[k].next) { b = A[k].to; if(mark[b]) continue; dfs(b); subtree[point]+=subtree[b]+1; dp[point]=max(dp[point],subtree[b]+1); } dp[point]=max(dp[point],n-subtree[point]-1); return 0;}int main(){int i,j,T;scanf("%d",&T);while(T--){scanf("%d",&n);tot=1;memset(list,-1,sizeof(list));memset(mark,0,sizeof(mark));for(i=1;i<n;i++){int a,b;scanf("%d%d",&a,&b);add(a,b);add(b,a);}memset(dp,0,sizeof(dp));memset(subtree,0,sizeof(subtree));memset(mark,0,sizeof(mark));dfs(1);int Min=3000000,k;for(i=1;i<=n;i++)if(dp[i]<Min){Min=dp[i];k=i;}printf("%d %d\n",k,Min);}return 0;}
0 0
- poj 1655 树形DP
- poj 1655 树形dp
- poj 1655(树形dp)
- POJ-1655-树形dp
- poj 1655 简单树形dp
- POJ 1655 简单树形dp
- poj 1655树形dp && poj 3107 &&poj 2378 && poj 3140
- 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
- poj 1655 Balancing Act(树形DP)
- (简单) 树形dp POJ 1655 Balancing Act
- Balancing Act - POJ 1655 树形dp
- poj 1655(树形DP 求解重心)
- poj 1655 Balancing Act (树形dp)
- POJ 1655 - Balancing Act(树形DP)
- 中缀表达式转后缀表达式的步骤描述
- x210v3开发板u-boot-2012.10移植之十---串口摇身一变控制台
- 黑马程序员——java基础——集合框架(一:单列集合(Collection体系))
- Python之正则表达式进阶篇
- OOAD 学习笔记 十
- poj 1655 树形dp
- GIS空间分析 对栅格数据分析,距离分析,点位置查找
- 第一个maven构建
- 图力导引布局算法
- ImageIcon & Image - 在组件上打印图片
- Netty IO线程模型学习总结
- 正则表达式与文本格式化处理
- HDU1728---逃离迷宫
- android判断edittext是否为空