poj2378

/***************************************************************\
 *Author:Hu Wenbiao
 *Created Time: Tue 17 Aug 2010 05:56:33 PM CST
 *File Name: main.cpp
 *Description:树状dp。跟poj1655几乎相同
\***************************************************************/
//*========================*Head File*========================*\\

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*----------------------*Global Variable*----------------------*/
int first[10010],N,X,Y,dp[10010],tot;
struct Edge{
 int to,next;
}edge[20010];
//*=======================*Main Program*=======================*//
using namespace std;

void insertedge(int X,int Y){
 edge[tot].to=Y;
 edge[tot].next=first[X];
 first[X]=tot++;
 edge[tot].to=X;
 edge[tot].next=first[Y];
 first[Y]=tot++;
}
int tree_dp(int from,int now){
 int sum=1,_max=0,tmp;
 int p=first[now],to;
 while(p!=-1){
  to=edge[p].to;
  p=edge[p].next;
  if(to==from)continue;
  tmp=tree_dp(now,to);
  if(_max<tmp)
    _max=tmp;
  sum+=tmp;
 }
 dp[now]=N-sum>_max?N-sum:_max;
 return sum;
}
int main(){
 //freopen("input","r",stdin);
 while(scanf("%d",&N)!=EOF){
  memset(first,-1,sizeof(first));
  tot=0;
  for(int i=1;i<N;i++){
   scanf("%d%d",&X,&Y);
   insertedge(X,Y);
  }
  tree_dp(0,1);
  for(int i=1;i<=N;i++){
   if(dp[i]<=N/2)
     printf("%d\n",i);
  }
 }
}
posted @ 2010-08-17 18:26  open source  阅读(193)  评论(0编辑  收藏