题目链接

题目

题目描述

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. 【荐】开源Winform控件库:花木兰控件库

    微信好友推荐,挺好看的Winfrom控件库,下面来看看. 介绍 基于 C#(语言) 4.0 . VS2019 . Net Framework 4.0(不包括Net Framework 4.0 Clie ...

  2. C++初始化列表时,形参和实参名可以一样,编译器可以识别

    在这里初始化列表直接用age(age)即可,用this->age(age)反而会出错,C++不允许在成员初始化列表中使用this关键字来初始化类成员 class Person { public: ...

  3. [转帖]oracle查询表变化量

    根据变化量,可确定表的繁忙度,以及作为判断可能数据增长的对象. select obj.owner, obj.object_name, to_char(sn.BEGIN_INTERVAL_TIME,'y ...

  4. [转帖]关于面试时HA(RAC)会问到的一些问题

    1.什么是RAC(Real Application Cluster)? RAC(Real Application Cluster)是Oracle数据库的一种部署架构,它将多个数据库服务器连接在一起,共 ...

  5. [转帖]使用Rclone实现minio数据的迁移

    使用Rclone实现minio数据的迁移 一.准备 1.1 使用工具 rclone:开源的对象存储在线迁移工具,用于文件和目录的同步,支持阿里云的oss.minio .亚马逊S3 等. 1.2 注意事 ...

  6. 【转帖】Meta 推出大型语言模型 LLaMA,比 GPT3.5 性能更高

    https://finance.sina.com.cn/wm/2023-02-28/doc-imyihfvp8075151.shtml ChatGPT 的爆火使得大家对 AI 进行了深度的讨论,大厂们 ...

  7. [转帖]Linux内核网络中的软中断ksoftirqd

    https://zhuanlan.zhihu.com/p/361976930 1. 前言 之前分享过Linux内核网络数据包的接收过程,当执行到网卡通过硬件中断(IRQ)通知CPU,告诉它有数据来了, ...

  8. C# WPF 开发一个 Emoji 表情查看软件

    微软在发布 Windows 11 系统的时候,发布过一个开源的 Emoji 表情 fluentui-emoji .因为我经常需要里面的一些表情图片,在仓库一个个查找特别的不方便,所以我做了一个表情查看 ...

  9. TypeScript中的元组 Tuple

    元组类型 // 元组类型:表示一个已知元素数量和类型的数组,各元素的类型不必相同 let undata: [string, '男'| '女']; //已知数量是两个.类型分别是字符串和男或者女 und ...

  10. package.json中^,~的详细说明

    场景描述 在package.json这个文件中,我们经常可以看见这样的信息 但是我们很少注意的是 版本前面的 ^ 到底是什么意思 今天我们就来讲一下(端好小板凳) "dependencies ...