题目链接

题目

题目描述

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:



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;
}

NC51222 Strategic game的更多相关文章

  1. HDU1054 Strategic Game——匈牙利算法

    Strategic Game Bob enjoys playing computer games, especially strategic games, but sometimes he canno ...

  2. DDD:Strategic Domain Driven Design with Context Mapping

    Introduction Many approaches to object oriented modeling tend not to scale well when the application ...

  3. poj 1463 Strategic game DP

    题目地址:http://poj.org/problem?id=1463 题目: Strategic game Time Limit: 2000MS   Memory Limit: 10000K Tot ...

  4. UVA 1292 十二 Strategic game

    Strategic game Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Sta ...

  5. hdu---(1054)Strategic Game(最小覆盖边)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. HDU 1054:Strategic Game

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. POJ1463:Strategic game(树形DP)

    Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot f ...

  8. hdoj 1054 Strategic Game【匈牙利算法+最小顶点覆盖】

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. Making the Elephant Dance: Strategic Enterprise Analysis

    http://www.modernanalyst.com/Resources/Articles/tabid/115/ID/2934/categoryId/23/Making-the-Elephant- ...

  10. (hdu step 6.3.1)Strategic Game(求用最少顶点数把全部边都覆盖,使用的是邻接表)

    题目: Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. Socket 如何处理粘包

    Socket 如何处理粘包 什么是粘包什么是半包? 粘包: 比如发送了AA BB 两条消息,但是另一方接收到的消息却是AAB,像这种一次性读取了俩条数据的情况就是粘包 半包: 比如发送的消息是ABC时 ...

  2. Angular系列教程之观察者模式和RxJS

    .markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...

  3. 小技巧:WIndows快速创建文件夹

    快速创建文件夹的技巧 1.首先创建文本文档将扩展名更改为.bt,mkdir.bat 2.写入创建文件夹的代码 md 文件夹1 文件夹2 文件夹3 pause 3.双击执行mkdir.bat

  4. 【Git】用法小记

    解决windows环境下的CRLF与unix环境下的LF问题,windows提交时CRLF=>LF,签出时LF=>CRLF,unix环境保留 git config --global cor ...

  5. css - absolute居中

    position:absolut; left:50%; top:50%; margin-left:  -(自身一半宽度); margin-top: -(自身一半高度)

  6. [转帖]060-轻量级基于curl的seafile上传脚本

    https://anjia0532.github.io/2021/04/07/seafile-client-curl/ 这是坚持技术写作计划(含翻译)的第 60 篇,定个小目标 999,每周最少 2 ...

  7. [转帖]TiDB-merge region相关问题

    一.开启region merge # 控制 Region Merge 的 size 上限,当 Region Size 大于指定值时 PD 不会将其与相邻的 Region 合并 pd-ctl confi ...

  8. [转帖]TiKV & TiDB相关笔记

    https://www.jianshu.com/p/1141be233bb2 一.TiKV存储 简述 通过单机的 RocksDB,TiKV 可以将数据快速地存储在磁盘上:通过 Raft,将数据复制到多 ...

  9. [转帖] 容器内的Linux诊断工具0x.tools

    https://www.cnblogs.com/codelogs/p/16242999.html 原创:扣钉日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处. 简介# Linux上有 ...

  10. nginx 最简单的在同一个配置文件里面将http 监听的端口转发到其他端口的方法

    今天发现一个问题, 我这边修改了nginx 的listen的端口之后 应用出现了问题 无法使用. 想到之前曾经试验过 tcp 的 proxy 所以就想到直接再配置文件的默认添加一句话 启动TCP的 端 ...