ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

NC51222 Strategic game

2022-08-23 19:34:28  阅读:190  来源: 互联网

标签: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
来源: https://www.cnblogs.com/BlankYang/p/16617475.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有