树dp:边覆盖,点覆盖

问题描述

对于一棵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+5;
int n,f[maxn][3];
//0->儿子,1->父亲,2->自己 
inline int min(int a,int b){
    return(a<b?a:b);
}
inline void read(int &ans){
    ans=0;int b=1;
    char x=getchar();
    while(x<'0' || '9'<x){
    if(x=='-')b=-1;
    x=getchar();
    }
    while('0'<=x && x<='9'){
        ans=(ans<<3)+(ans<<1)+x-'0';
        x=getchar();
    }
    ans*=b;
}
int edge_count=0,to[maxn*2],next[maxn*2],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=0;
    for(int i=first[root];i;i=next[i]){
if(to[i]==fa)continue;

search(to[i],root);
f[root][2]+=f[ to[i] ][1];
f[root][1]+=min(f[ to[i] ][2],f[ to[i] ][0]);//+最小值

if( f[ to[i] ][2]-f[ to[i] ][0] < f[pos][2]-f[pos][0] )
pos=to[i];
}    
    f[root][2]++;
    if(f[root][1]>f[root][2])f[root][1]=f[root][2];
    bool fd=1;
    for( int i=first[root];i;i=next[i] ){
    if(to[i]==fa)continue;
    fd=0;
    if(to[i]==pos){
        f[root][0]+=f[pos][2];
        }
    else{
        f[root][0]+=min(f[to[i]][0],f[to[i]][2]);
        }
    }
f[root][0]+=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=1,a,b;i<n;i++){
        read(a);read(b);
        add_edge(a,b);
        add_edge(b,a);
    } 
    f[0][2]=0x7fffffff;
    search(1,0);
    printf("%d",min(f[1][0],min(f[1][1]+1,f[1][2])));
    return 0;
}

 

转载于:https://www.cnblogs.com/a-blog-of-taojiayi-2003/p/10656772.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值