P2016 战略游戏

时间:2019-11-10
本文章向大家介绍P2016 战略游戏,主要包括P2016 战略游戏使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

树形DP,求最小点覆盖集

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define maxn 1510

using namespace std;

struct node
{
    int ed,nxt;
};
node edge[maxn<<1];
int n,m,cnt,first[maxn];
int dp[maxn][2];

inline void add_edge(int s,int e)
{
    ++cnt;
    edge[cnt].ed=e;
    edge[cnt].nxt=first[s];
    first[s]=cnt;
    return;
}

inline void dfs(int now,int fa)
{
    for(register int i=first[now];i;i=edge[i].nxt)
    {
        int e=edge[i].ed;
        if(e!=fa)
        {
            dfs(e,now);
            dp[now][0]+=dp[e][1];
            dp[now][1]+=min(dp[e][0],dp[e][1]);
        }
    }
    dp[now][1]+=1;
    return;
}

int main()
{
    scanf("%d",&n);
    for(register int i=1;i<=n;++i)
    {
        int now,tot;
        scanf("%d%d",&now,&tot);
        now++;
         for(register int j=1;j<=tot;++j)
         {
             int e;
             scanf("%d",&e);
             add_edge(now,e+1);
            add_edge(e+1,now); 
        }
    }
    dfs(1,0);
    printf("%d\n",min(dp[1][0],dp[1][1]));
    return 0;
} 

原文地址:https://www.cnblogs.com/Hoyoak/p/11831301.html