Strategic Game

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10035    Accepted Submission(s): 4691

Problem Description
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.

The input file 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_identifier1 node_identifier2 ... node_identifier
or
node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.

For example for the tree:

the solution is one soldier ( at the node 1).

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 table:

 
Sample Input
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)
 
Sample Output
1 2

题目大意:

给你一棵树,问你最少占几个点,就可以覆盖所有边。(占住某个点,由这点出发的所有边都称作被覆盖)

树形DP基础题。

dp[i][0]表示以i为根节点的子树,在不占i的情况下,最少需要占多少个点;

dp[i][1]表示以i为根节点的子树,在占i的情况下,最少需要占多少个点;

则状态转移方程:

dp[i][0]=∑dp[son][1];

dp[i][1]=∑min(dp[son][0],dp[son][1]);

可见是由下至上转移的,所以DFS就OK啦。

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm> using namespace std; const int maxn=; int to[maxn*+];
int nex[maxn*+];
int head[maxn*+]; int dp[maxn+][];
int vis[maxn+]; void dfs(int x)
{
vis[x]=;
bool flag=;
for(int i=head[x];i!=-;i=nex[i])
{
if(!vis[to[i]])
{
dfs(to[i]);
flag=;
}
}
if(!flag)
{
dp[x][]=;
return;
}
for(int i=head[x];i!=-;i=nex[i])
{
dp[x][]+=dp[to[i]][];
dp[x][]+=min(dp[to[i]][],dp[to[i]][]);
}
dp[x][]+=;
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(head,-,sizeof(head));
for(int i=,a,b,c,cnt=;i<=n;i++)
{
scanf("%d:(%d)",&a,&c);
while(c--)
{
scanf("%d",&b);
to[cnt]=a;nex[cnt]=head[b];head[b]=cnt++;
to[cnt]=b;nex[cnt]=head[a];head[a]=cnt++;
}
} memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
dfs(); printf("%d\n",min(dp[][],dp[][]));
}
return ;
}

还有一道类似的入门题,hdu1520,附ac代码。

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm> using namespace std; const int maxn=; int weight[maxn+];
int isson[maxn+]; int to[maxn+];
int nex[maxn+];
int head[maxn+]; int dp[maxn][]; void dfs(int x)
{
if(head[x]==-)
{
dp[x][]=;
dp[x][]=weight[x];
//printf("%d %d %d\n%d %d %d\n",x,0,dp[x][0],x,1,dp[x][1]);
return;
}
for(int i=head[x];i!=-;i=nex[i])
dfs(to[i]);
for(int i=head[x];i!=-;i=nex[i])
{
dp[x][]+=max(dp[to[i]][],dp[to[i]][]);
dp[x][]+=dp[to[i]][];
}
dp[x][]+=weight[x];
//printf("%d %d %d\n%d %d %d\n",x,0,dp[x][0],x,1,dp[x][1]);
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d",weight+i);
memset(isson,,sizeof(isson));
memset(head,-,sizeof(head));
for(int i=,a,b,cnt=;i<=n;i++)
{
scanf("%d%d",&a,&b);
if(i==n)
break;
to[cnt]=a;
nex[cnt]=head[b];
head[b]=cnt++;
isson[a]=;
}
int origin;
for(int i=;i<=n;i++)
if(!isson[i])
{
origin=i;
break;
}
//printf("%d\n",origin); memset(dp,,sizeof(dp));
dfs(origin); printf("%d\n",max(dp[origin][],dp[origin][]));
}
return ;
}

hdu 1054 Strategic Game (简单树形DP)的更多相关文章

  1. HDU 1054 Strategic Game(树形DP)

    Problem Description Bob enjoys playing computer games, especially strategic games, but sometimes he ...

  2. hdu 1054 Strategic Game 经典树形DP

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. HDU 1054 Strategic Game(树形DP)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  5. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  6. HDU - 1054 Strategic Game(二分图最小点覆盖/树形dp)

    d.一颗树,选最少的点覆盖所有边 s. 1.可以转成二分图的最小点覆盖来做.不过转换后要把匹配数除以2,这个待细看. 2.也可以用树形dp c.匈牙利算法(邻接表,用vector实现): /* 用ST ...

  7. HDU 1054 Strategic Game (树形dp)

    题目链接 题意: 给一颗树,用最少的点覆盖整棵树. 每一个结点可以防守相邻的一个边,求最少的点防守所有的边. 分析: 1:以当前节点为根节点,在该节点排士兵守护道路的最小消耗.在这种情况下,他的子节点 ...

  8. HDU 1054 Strategic Game(最小点覆盖+树形dp)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=106048#problem/B 题意:给出一些点相连,找出最小的点数覆盖所有的 ...

  9. hdu 1520Anniversary party(简单树形dp)

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. robatframework+jenkins+email集成部署方案

    准备工作: 1.jenkins.war包 下载地址:https://jenkins.io/zh/download/ 2.Jdk1.8 下载地址:http://www.oracle.com/techne ...

  2. numpy和matplotlib下载中出现的问题

    在安装numpy的时候遇到如下所示的错误: 经过几个小时的查找,最终发现是pygame的路径不对导致.将pygame的具体路径加上后,问题解决.实施如下:得出一个结论:路径很重要,千万得小心哦. 报错 ...

  3. nginx 负载均衡简单配置

    配置要求: 三台服务器 127.0.0.1       主负载(把访问请求分给主机池) 127.0.0.2       主机2 127.0.0.3       主机3 第一步: 配置127.0.0.1 ...

  4. 模型量化原理及tflite示例

    模型量化 什么是量化 模型的weights数据一般是float32的,量化即将他们转换为int8的.当然其实量化有很多种,主流是int8/fp16量化,其他的还有比如 二进制神经网络:在运行时具有二进 ...

  5. Spring Cloud Alibaba(五)RocketMQ 异步通信实现

    本文探讨如何使用 RocketMQ Binder 完成 Spring Cloud 应用消息的订阅和发布. 介绍 RocketMQ 是一款开源的分布式消息系统,基于高可用分布式集群技术,提供低延时的.高 ...

  6. zabbix自动发现 url 以及对http返回状态码监控实现 告警

    2019-06-04 18:39:12 目的:批量监控业务URL的返回状态码,通过zabbix监控判断业务好坏. 1.zabbix安装 请查看此永久链接:https://www.cnblogs.com ...

  7. CentOS 7 网卡 bond 配置

    第一块网卡配置 [root@localhost network-scripts]# cat ifcfg-eth0 TYPE=Ethernet BOOTPROTO=none USERCTL=no DEV ...

  8. ViewGroup dispatchTouchEvent方法中 mFirstTouchTarget标志是否为空的含义

    在ViewGroup dispatchTouchEvent方法中首次出现mFirstTouchTarget的语句为: if (actionMasked == MotionEvent.ACTION_DO ...

  9. Spring Boot 自动装配(二)

    目录 目录 前言 1.起源 2.Spring Boot 自动装配实现 2.1.@EnableAutoConfiguration 实现 2.1.1. 获取默认包扫描路径 2.1.2.获取自动装配的组件 ...

  10. 安装破解版IntelliJ IDEA

    1.下载IntelliJ IDEA http://www.jetbrains.com/idea/download/#section=windows 选择Ultimate版本 2.注册码破解 http: ...