【题解】Luogu SP1435 PT07X - Vertex Cover

原题传送门

求树的最小点覆盖,就是一个树形dp

类似于没有上司的舞会

dp的状态为\(f[i][0/1]\),表示i节点是否选择

边界是\(f[x][0]=0\)\(f[x][1]=1\)

转移方程是

\(f[i][0]=\sum_{j=son[i]} f[j][1]\)

\(f[i][1]=\sum_{j=son[i]} Min(f[j][0],f[j][1])\)

最后答案就是\(Min(f[1][0],f[1][1])\)

#include <bits/stdc++.h>
#define getchar nc
#define N 100005
using namespace std;
 inline char nc(){
    static char buf[100000],*p1=buf,*p2=buf; 
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; 
}
inline int read()
{
    register int x=0,f=1;register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return x*f;
}
inline void write(register int x)
{
    if(!x)putchar('0');if(x<0)x=-x,putchar('-');
    static int sta[20];register int tot=0;
    while(x)sta[tot++]=x%10,x/=10;
    while(tot)putchar(sta[--tot]+48);
}
inline int Min(register int a,register int b)
{
    return a<b?a:b;
}
struct node{
    int to,next;
}e[N<<1];
int head[N],cnt=0;
inline void add(register int u,register int v)
{
    e[++cnt]=(node){v,head[u]};
    head[u]=cnt;
}
int n,f[N][2];
inline void dfs(register int u,register int fa)
{
    f[u][1]=1;
    f[u][0]=0;
    for(register int i=head[u];i;i=e[i].next)
    {
        int v=e[i].to;
        if(v==fa)
            continue;
        dfs(v,u);
        f[u][1]+=Min(f[v][0],f[v][1]);
        f[u][0]+=f[v][1];
    }
}
int main()
{
    n=read();
    for(register int i=1;i<n;++i)
    {
        int u=read(),v=read();
        add(u,v),add(v,u);
    }
    dfs(1,0);
    write(Min(f[1][0],f[1][1]));
    return 0;
}
posted @ 2019-01-28 12:28  JSOI爆零珂学家yzhang  阅读(235)  评论(0编辑  收藏  举报