首页 > 其他分享 >NC51222 Strategic game

NC51222 Strategic game

时间:2022-08-23 19:22:43浏览次数:93  
标签:node NC51222 number Strategic game roads input identifier dp

题目链接

题目

题目描述

Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?

Your program should find the minimum number of soldiers that Bob has to put for a given tree.

For example for the tree:
img
the solution is one soldier ( at the node 1).

输入描述:

The input contains several data sets in text format. Each data set represents a tree with the following description:
the number of nodes
the description of each node in the following format
node_identifier:(number_of_roads) \(node\_identifier_1\) \(node\_identifier_2\) ... \(node\_identifier_{number\_of\_roads }\)
or
node_identifier:(0)
The node identifiers are integer numbers between 0 and n-1, for n nodes \((0 \lt n \leq 1500)\) ;the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.

输出描述

The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following:

示例1

输入

4
0:(1) 1
1:(2) 2 3
2:(0)
3:(0)
5
3:(3) 1 4 2
1:(1) 0
2:(0)
0:(0)
4:(0)

输出

1
2

题解

知识点:树形dp

题目要求最少点覆盖所有边(最小点覆盖),一个点能覆盖所连的所有边,所以有如下情况。

以 \(1\) 为根,设 \(dp[u][0/1]\) 表示以 \(u\) 为根的子树,\(u\) 的状态是不选/选的最小值。转移方程为:

\[\left \{ \begin{array}{l} dp[u][0] = \sum dp[v_i][1]\\ dp[u][1] = \sum \min(dp[v_i][0],dp[v_i][1]) \end{array} \right . \]

表示 \(u\) 不选则孩子必须选;\(u\) 选了孩子可选可不选,取最小值。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>

using namespace std;

vector<int> g[1507];
int dp[1507][2];

///快读
template<class T>
inline void read(T &val) {
    T x = 0, f = 1;char c = getchar();
    while (c < '0' || c>'9') { if (c == '-') f = -1;c = getchar(); }///整数符号
    while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48);c = getchar(); }///挪位加数
    val = x * f;
}

void dfs(int u, int fa) {
    for (auto v : g[u]) {
        if (v == fa) continue;
        dfs(v, u);
        dp[u][0] += dp[v][1];
        dp[u][1] += min(dp[v][0], dp[v][1]);
    }
    dp[u][1]++;
}

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n;
    while (~scanf("%d", &n)) {
        memset(dp, 0, sizeof(dp));
        for (int u = 0;u < n;u++) g[u].clear();
        for (int i = 1;i <= n;i++) {
            int u, cnt;
            read(u);
            read(cnt);
            for (int j = 1, v;j <= cnt;j++) {
                read(v);
                g[u].push_back(v);
                g[v].push_back(u);
            }
        }
        dfs(0, -1);
        cout << min(dp[0][0], dp[0][1]) << '\n';
    }
    return 0;
}

标签:node,NC51222,number,Strategic,game,roads,input,identifier,dp
From: https://www.cnblogs.com/BlankYang/p/16617475.html

相关文章

  • Game Engine MetaData Creation With Clang
    ALittleContexttoStart我的hobby引擎使用一个系统,任何类或者结构体可以有metadata,但是这不是严格必须的。除此之外,每个metadata开启的类型,并不要求去有一个虚函数表......
  • Link with Game Glitch(负环)
    题意有\(n\)个物品,\(m\)个转换,每\(ka_i\)个\(b_i\)类物品可以换\(w\cdotkc_i\)个\(d_i\)类物品。其中\(k\)为任意正实数。求最大的\(0\leqw\leq1\)使得不存在一种......
  • 2022/8/19日测试 (内含Ticket Game,生日蛋糕,最优贸易,装满的油箱,道路游戏)
    TicketGame标签:思维--------------------------------------------------------------------------------------------------------Alice和Bob生活在Berland。Berland......
  • Game Theory
    GameTheory目录博弈的基本概念组合游戏SG函数经典组合游戏模型导言:博弈的基本概念博弈论是研究具有斗争和竞争性质现象的数学理论和方法,博弈论,又称为对策论(GameTh......
  • CF1719A Chip Game 题解
    题目传送门。思路当其中一个人不能动的时候,这个人一定位于点\((n,m)\)上。令点\((n,m)\)为终点。当\(n\)和\(m\)都是奇数或当\(n\)和\(m\)都是偶数时,赢的人......
  • 搭建UnityGameFramework框架最低需求项目
    1、下载GameFramework包进入官网的下载页面下载2021.05.31版本https://gameframework.cn/download/2、新建Unity项目,然后把包导入3、新建Editor文件夹,并创建GameFr......
  • CF1498F Christmas Game
    problem一棵树,有root,一个点只能向根跳k步直到不能走,问先手必败还是必胜。root从1到n,回答n次solution一次的话就是一个阶梯nim。多次的话,就要换根。换根的话,虽然会全......
  • 多校8 D Poker Game: Decision
    problem暴力sg,打牌code#include<bits/stdc++.h>#defineFOR(i,a,b)for(inti=a;i<=b;++i)#definelllonglongusingnamespacestd;constint_=1e6+7;//const......
  • NC24870 [USACO 2009 Dec G]Video Game Troubles
    题目链接题目题目描述FarmerJohn'scowslovetheirvideogames!FJnoticedthatafterplayingthesegamesthathiscowsproducedmuchmoremilkthanusual,s......
  • 再探 游戏 《 2048 》 —— AI方法—— 缘起、缘灭(6) ——《Temporal Difference Learn
    《2048》游戏在线试玩地址:https://play2048.co/  如何解决《2048》游戏源于外网的一个讨论帖子,而这个帖子则是讨论如何解决该游戏的最早开始,可谓是“缘起”:Whatis......