题意:
给一颗树,可以删除树中一点,求删除那些点后最大的联通块内点数小于等于总点数一半,输出这些点。
思路:
定义dp[u]表示将u删去后最大联通块的点数:dp[u] = max ( n-sizes[u] , max( son[u] ) ),将dp[u]<=n的点输出。
C++代码:
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 100010;
const int maxm = 100010;
const int inf = 0x3f3f3f3f;
int n,tol,head[maxn];
struct edge
{
int to,next;
}es[maxm];
void addedge( int u , int v )
{
es[tol].to = v;
es[tol].next = head[u];
head[u] = tol++;
}
int sizes[maxn];
int son[maxn];
int dp[maxn];
void dfs1( int u , int f )
{
sizes[u] = 1;
for ( int i=head[u] ; i!=-1 ; i=es[i].next )
{
int v = es[i].to;
if ( v!=f )
{
dfs1( v , u );
sizes[u] += sizes[v];
son[u] = max ( son[u] , sizes[v] );
}
}
dp[u] = max ( n-sizes[u] , son[u] );
}
int main()
{
while( scanf( "%d" , &n )==1 )
{
tol = 0; memset( head , -1 , sizeof(head) );
for ( int i=1 ; i<n ; i++ )
{
int u,v; scanf( "%d%d" , &u , &v );
addedge( u , v );
addedge( v , u );
}
memset ( dp , inf , sizeof(dp) );
memset ( son , 0 , sizeof(son) );
dfs1 ( 1 , 0 );
for ( int i=1 ; i<=n ; i++ )
if ( dp[i]<=n/2 ) printf ( "%d\n" , i );
}
return 0;
}