P2016 战略游戏——树形DP大水题

P2016 战略游戏

树形DP 入门题吧(现在怎么是蓝色标签搞不懂);

注意是看见每一条边而不是每一个点(因为这里错了好几次);

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=3010;
int pre[maxn],last[maxn],other[maxn],l;

void add(int x,int y)
{
    l++;
    pre[l]=last[x];
    last[x]=l;
    other[l]=y;
}

int n;
int f[maxn][2];

void dfs(int x,int fa)
{
    f[x][1]=1;f[x][0]=0;
    for(int p=last[x];p;p=pre[p])
    {
        int v=other[p];
        if(v==fa) continue;
        dfs(v,x);
        f[x][1]+=min(f[v][0],f[v][1]);
        f[x][0]+=f[v][1];
    }
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x,k,y; 
        scanf("%d%d",&x,&k);
        x++;
        for(int j=1;j<=k;j++) 
        {
            scanf("%d",&y);
            y++;
            add(x,y);
            add(y,x);
        }
    }
    dfs(1,0);
    printf("%d",min(f[1][1],f[1][0]));
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/WHFF521/p/11756615.html