• 刷题记录:牛客NC51222Strategic 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).
    输入:
    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    经典的树形dp的题目

    主要思路:

    1. 对于这道题,我们使用 d p [ u ] [ 0 / 1 ] dp[u][0/1] dp[u][0/1]分别表示当前的节点放置士兵 / / /不放置士兵的最少士兵数.
    2. 那么我们很容易就能想到转移方程,对于当前结点,假设我放置士兵的话,那么对于它的儿子来说它可以放士兵,也可以不放士兵,即

    d p [ u ] [ 0 ] + = m i n ( d p [ v ] [ 0 ] , d p [ v ] [ 1 ] ) dp[u][0]+=min(dp[v][0],dp[v][1]) dp[u][0]+=min(dp[v][0],dp[v][1])

    1. 对于当前结点,假设我不放置士兵的话,那么对于它的儿子来说,每一个儿子都需要放置士兵,不然的话就无法管理那一条路了.即

    d p [ u ] [ 1 ] + = d p [ v ] [ 0 ] dp[u][1]+=dp[v][0] dp[u][1]+=dp[v][0]

    然后这道题就巧妙的解决了

    下面是具体的代码部分:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    typedef long long ll;
    #define inf 0x3f3f3f3f
    #define root 1,n,1
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    inline ll read() {
    	ll x=0,w=1;char ch=getchar();
    	for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') w=-1;
    	for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
    	return x*w;
    }
    #define maxn 1000000
    #define ll_maxn 0x3f3f3f3f3f3f3f3f
    const double eps=1e-8;
    int n;int dp[maxn][4];
    vector<int>edge[maxn];
    void dfs(int u,int pre_u) {
    	dp[u][0]=1;
    	for(int i=0;i<edge[u].size();i++) {
    		int v=edge[u][i];
    		if(v==pre_u) continue;
    		dfs(v,u);
    		dp[u][0]+=min(dp[v][0],dp[v][1]);
    		dp[u][1]+=dp[v][0];
    	}
    	return ;
    }
    int main() {
    	while(scanf("%d",&n)!=EOF) {
    		memset(dp,0,sizeof(dp));
    		for(int i=0;i<=n;i++) edge[i].clear();
    		int u,v,num;
    		for(int i=1;i<=n;i++) {
    			scanf("%d:(%d)",&u,&num);
    			for(int j=1;j<=num;j++) {
    				v=read();
    				edge[u].push_back(v);
    				edge[v].push_back(u);
    			}
    		}
    		dfs(0,-1);
    		cout<<min(dp[0][1],dp[0][0])<<endl;
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
  • 相关阅读:
    Linux学习-44-虚拟内存、物理内存和swap分区的作用
    SqueezeNet 一维,二维网络复现 pytorch 小白易懂版
    组合问题看透回溯法
    代码随想录二刷day15
    Source Insight是一个面向项目开发的程序编辑器和代码浏览器丨产品简介
    无涯教程-Python机器学习 - Stochastic Gradient Boosting函数
    LeetCode 929. Unique Email Addresses
    非计算机科班如何丝滑转码?
    前端笔试题总结,带答案和解析(三)
    25% 的开发者认为 Rust 是最佳替代,最新 Go 开发者调查报告出炉
  • 原文地址:https://blog.csdn.net/yingjiayu12/article/details/127947939