POJ - 2378 Tree Cutting(树形DP)
#include<iostream>
#include<vector>
using namespace std;const int N = 10010;
vector<int> g[N];
int sum[N],maxn[N];void dfs(int u,int fa)
{
for(int i=0;i<(int)g[u].size();i++){
if(g[u][i]==fa) continue;dfs(g[u][i],u);sum[u]+=sum[g[u][i]]+1;maxn[u]=max(maxn[u],sum[g[u][i]]+1); }
}int main()
{
int n;cin>>n;for(int i=0;i<n-1;i++){
int a,b;cin>>a>>b;g[a].push_back(b);g[b].push_back(a);}dfs(1,-1);for(int i=1;i<=n;i++) if(maxn[i]<=n/2&&n-(sum[i]+1)<=n/2)cout<<i<<endl;return 0;
}