
#493. 求树的最小支配集
问题描述
对于一棵n个结点的无根树,求它的最小支配集。 最小支配集:指从所有顶点中取尽量少的点组成一个集合,使得剩下的所有点都与取出来的点有边相连。顶点个数最小的支配集被称为最小支配集。
输入格式
第一行一个整数n,表示结点数。接下来n-1行,每行两个整数a,b,表示结点a和b有边。
输出格式
一行,一个整数,表示最小支配集中元素的个数。
输入样例
9
2 5
2 7
2 8
9 2
3 2
3 1
3 6
4 3
输出样例
2
限制与预定
1 < n <=100000
时间限制:1s
空间限制:128mb
#include<cstdio>
using namespace std;
const int maxn=1e5+;
int n,f[maxn][];
//0->儿子,1->父亲,2->自己
inline int min(int a,int b){
return(a<b?a:b);
}
inline void read(int &ans){
ans=;int b=;
char x=getchar();
while(x<'' || ''<x){
if(x=='-')b=-;
x=getchar();
}
while(''<=x && x<=''){
ans=(ans<<)+(ans<<)+x-'';
x=getchar();
}
ans*=b;
}
int edge_count=,to[maxn*],next[maxn*],first[maxn];
inline void add_edge(int x,int y){
edge_count++;
to[edge_count]=y;
next[edge_count]=first[x];
first[x]=edge_count;
}
void search(int root,int fa)
{
int pos=;
for(int i=first[root];i;i=next[i]){
if(to[i]==fa)continue; search(to[i],root);
f[root][]+=f[ to[i] ][];
f[root][]+=min(f[ to[i] ][],f[ to[i] ][]);//+最小值 if( f[ to[i] ][]-f[ to[i] ][] < f[pos][]-f[pos][] )
pos=to[i];
}
f[root][]++;
if(f[root][]>f[root][])f[root][]=f[root][];
bool fd=;
for( int i=first[root];i;i=next[i] ){
if(to[i]==fa)continue;
fd=;
if(to[i]==pos){
f[root][]+=f[pos][];
}
else{
f[root][]+=min(f[to[i]][],f[to[i]][]);
}
}
f[root][]+=fd;
//if(root==9||root==8||root==7||root==5)printf("%d %d",root,f[root][0]);
//if(f[root][0]||f[root][1]||f[root][2] )printf("%d",root);
//if(root==1)printf("%d",f[1][0]);
}
int main()
{
//freopen("2.in","r",stdin); read(n);
for( int i=,a,b;i<n;i++){
read(a);read(b);
add_edge(a,b);
add_edge(b,a);
}
f[][]=0x7fffffff;
search(,);
printf("%d",min(f[][],min(f[][]+,f[][])));
return ;
}