题目链接

题目

题目描述

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. 使用python的os.walk()对目标路径进行遍历

    需求背景 在使用python处理和扫描系统文件的过程中,经常要使用到目录或者文件遍历的功能,这里通过引入os.walk()的功能直接来实现这个需求. 使用示例 由于功能模块本身比较简单,这里直接提供一 ...

  2. 你老了,别搞IT了……

    你老了,别搞IT了-- [来源]

  3. asp.net core 开启gzip压缩

    // 第一步: 配置gzip与br的压缩等级为最优 services.Configure<BrotliCompressionProviderOptions>(options => { ...

  4. [转帖]global cache cr request等待事件分析及优化

    在RAC环境中,和全局调整缓存相关的最常见的等待事件无非就是:global cache cr request,global cache busy和equeue 在XX电信做了一次数据库巡检中发现,sp ...

  5. 一个监控数据的思考-sockets_used

    一个监控数据的思考-sockets_used 背景 最近跟踪一个项目问题. Grafana的监控了里面有一个tcp的使用监控 CurrEstab 的数据量是: 700-2000 左右 但是同时有一个非 ...

  6. [转帖]【Jmeter】Jmeter压力测试工具安装及使用教程(redis测试)

    摘自:https://www.cnblogs.com/monjeo/p/9330464.html 一.Jmeter下载 进入官网:http://jmeter.apache.org/ 1.第一步进入官网 ...

  7. [转帖]Linux:CPU频率调节模式以及降频方法简介

    概述 cpufreq的核心功能,是通过调整CPU的电压和频率,来兼顾系统的性能和功耗.在不需要高性能时,降低电压和频率,以降低功耗:在需要高性能时,提高电压和频率,以提高性能. cpufreq 是一个 ...

  8. [转帖]Nginx性能优化详解

    https://developer.aliyun.com/article/886146?spm=a2c6h.24874632.expert-profile.256.7c46cfe9h5DxWK 感觉文 ...

  9. [转帖] Linux查看日志文件写入速度的4种方法

    https://www.cnblogs.com/codelogs/p/16365448.html 简介# 有时,我们需要查看某个文件的增长速度,如日志文件,以此来感受系统的负载情况,因为一般情况下,日 ...

  10. 全球首个面向遥感任务设计的亿级视觉Transformer大模型

    作者:京东探索研究院 深度学习在很大程度上影响了遥感影像分析领域的研究.然而,大多数现有的遥感深度模型都是用ImageNet预训练权重初始化的,其中自然图像不可避免地与航拍图像相比存在较大的域差距,这 ...