NC51222 Strategic game
题目
题目描述
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\) 的状态是不选/选的最小值。转移方程为:
\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的更多相关文章
- HDU1054 Strategic Game——匈牙利算法
Strategic Game Bob enjoys playing computer games, especially strategic games, but sometimes he canno ...
- DDD:Strategic Domain Driven Design with Context Mapping
Introduction Many approaches to object oriented modeling tend not to scale well when the application ...
- poj 1463 Strategic game DP
题目地址:http://poj.org/problem?id=1463 题目: Strategic game Time Limit: 2000MS Memory Limit: 10000K Tot ...
- UVA 1292 十二 Strategic game
Strategic game Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Sta ...
- hdu---(1054)Strategic Game(最小覆盖边)
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1054:Strategic Game
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- POJ1463:Strategic game(树形DP)
Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot f ...
- hdoj 1054 Strategic Game【匈牙利算法+最小顶点覆盖】
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Making the Elephant Dance: Strategic Enterprise Analysis
http://www.modernanalyst.com/Resources/Articles/tabid/115/ID/2934/categoryId/23/Making-the-Elephant- ...
- (hdu step 6.3.1)Strategic Game(求用最少顶点数把全部边都覆盖,使用的是邻接表)
题目: Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- TCP 三次握手和四次挥手详解
转载请注明出处: TCP协议(Transmission Control Protocol) 面向连接的,可靠的,基于字节流的传输层通信协议 特点: 基于连接的:数据传输之前需要建立连接 全双工的:双向 ...
- JMS微服务开发示例(八)双机热备
双机热备,指两个一模一样的微服务,两个同时在运行,但是只有一个在工作,当工作中的微服务垮掉后,另一个会自行补上. 要实现这个,只需要设置 SingletonService = true. var mi ...
- MySQL高可用搭建方案之(MHA)
有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准https://blog.zysicyj.top 首发博客地址 原文地址 MHA架构介绍 MHA是Master High Av ...
- iftop的学习与使用
iftop的学习与使用 背景 前段时间一直进行netperf 等网络性能验证工具的学习与使用. 监控很多时候采用了 node-exporter + prometheus + grafana来进行观察 ...
- parquet极简学习
parquet极简学习 摘要 parquet的概念: Parquet文件是一种列式存储文件格式,广泛应用于大数据处理框架, 如Apache Hadoop和Apache Spark. 它通过将数据组织成 ...
- [转帖]TiDB Control 使用说明
https://docs.pingcap.com/zh/tidb/stable/tidb-control TiDB Control 是 TiDB 的命令行工具,用于获取 TiDB 状态信息,多用于调试 ...
- [转帖]自动化回归测试工具 —— AREX 上手实践
https://my.oschina.net/arextest/blog/8589156 AREX 是一款开源的自动化测试工具平台,基于 Java Agent 技术与比对技术,通过流量录制回放能力 ...
- [转帖]Spring Cloud Alibaba Nacos 注册中心使用教程
一. 什么是Nacos Nacos是一个更易于构建云原生应用的动态服务发现(Nacos Discovery ).服务配置(Nacos Config)和服务管理平台,集注册中心+配置中心+服务管理于一身 ...
- [转帖]JVM 参数
https://www.cnblogs.com/xiaojiesir/p/15636100.html 我们可以在启动 Java 命令时指定不同的 JVM 参数,让 JVM 调整自己的运行状态和行为,内 ...
- [转帖] mysql的timestamp会存在时区问题?
我感觉 这样理解也有点不对 timestamp 应该是不带时区 只是 UTC1970-1-1 的时间戳 但是展示时会根据时区做一下计算 date time 就不会做转换而已. 原创:打码日记(微信 ...