【洛谷P2016】战略游戏

题面

题解

树形\(dp\)(最大独立集)

\(f_{i,0/1}\)表示\(dp\)到第\(i\)个点,在这个点放了(没放)士兵的最小花费

直接转移即可。

代码

#include<cstdio>
#include<cstring>
#include<vector>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x));

namespace IO
{
    const int BUFSIZE = 1 << 20;
    char ibuf[BUFSIZE], *is = ibuf, *it = ibuf;
    inline char getchar() { if (is == it) it = (is = ibuf) + fread(ibuf, 1, BUFSIZE, stdin); return *is++; }
}

inline int read()
{
    int data = 0, w = 1;
    char ch = IO::getchar();
    while(ch != '-' && (ch < '0' || ch > '9')) ch = IO::getchar();
    if(ch == '-') w = -1, ch = IO::getchar();
    while(ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = IO::getchar();
    return data*w;
}

const int maxn(2000);
int f[maxn][2], n;

std::vector<int> G[maxn];
typedef std::vector<int>::iterator iter;

void dfs(int x)
{
    f[x][1] = 1; f[x][0] = 0;
    for(RG iter it = G[x].begin(); it != G[x].end(); ++it)
        dfs(*it), f[x][1] += std::min(f[*it][1], f[*it][0]), f[x][0] += f[*it][1];
}

int main()
{
#ifndef ONLINE_JUDGE
    file(cpp);
#endif
    n = read();
    for(RG int i = 1, x, num; i <= n; i++)
    {
        x = read() + 1; num = read();
        for(RG int i = 1; i <= num; i++) G[x].push_back(read() + 1);
    }
    dfs(1); printf("%d\n", std::min(f[1][0], f[1][1]));
    return 0;
}

转载于:https://www.cnblogs.com/cj-xxz/p/9839621.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值