【贪心】【树形DP】[POJ1463][HDU1054]Strategic game 战略游戏
来源:互联网 发布:集贤一中网络空间首页 编辑:程序博客网 时间:2024/05/16 12:33
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.
For example for the tree:
the solution is one soldier ( at the node 1).
Input
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_identifier1 node_identifier2 … node_identifiernumber_of_roads
or
node_identifier:(0)
The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.
Output
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:
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、二分图匹配,我只实现了前两种方法,最后一种请读者自行实现。
贪心
这道题一看这个图,就知道叶子节点绝对不能设立守卫,如果设立了,就不是最优解了。多画画图,我们可以找到一种规律,如下图例子:
因为叶子节点不能设立守卫,所以我们设立在叶子节点的父亲上,设立守卫的点标为绿色,守卫能守到的范围标为蓝色。
因为前面的点已经被标了,后面的点就没有选择,所以就只能表没有标过的父亲节点。
完成。这就是基本的贪心思路。
源代码
#include<cstdio>#include<cstring>const int N=1501;#define clear(a) memset(a,0,sizeof a)int l[N],s[N],vis[N],map[N][16],n;int getint(){ int x=0;char ch=getchar(); while(ch<'0'||ch>'9') ch=getchar(); while(ch>='0'&&ch<='9'){ x=x*10+ch-'0'; ch=getchar(); } return x;}int dfs(int u){ int place=0,r=0;vis[u]=1; for(int i=0;i<l[u];i++){ if(vis[map[u][i]]) continue; r+=dfs(map[u][i]); if(!s[map[u][i]]) place=1; } return r+(s[u]=place);}int main(){ while(~scanf("%d",&n)){ clear(l); clear(s); clear(vis); for(int i=1;i<=n;i++){ int u=getint(),e=getint(); while(e--){ int v=getint(); map[u][l[u]++]=v; map[v][l[v]++]=u; } } printf("%d\n",dfs(0)); }}
树形DP
直接给状态转移方程:
dp[u][0] = sum{dp[v][1]}; 代表u节点没有士兵
dp[u][1] = dp[u][1]+sum{dp[v][1],dp[v][0]}; 代表u节点有士兵
源代码
#include<vector>#include<cstdio>#include<cstring>using namespace std;vector <int> f[1501];int dp[1501][2];int n,m,k,x;bool vis[1501];void dfs(int u,int fa){ dp[u][1]=1; int len=f[u].size(); for(int i=0;i<len;i++){ int v=f[u][i]; if(v==fa) continue; dfs(v,u); dp[u][0]+=dp[v][1]; dp[u][1]+=min(dp[v][0],dp[v][1]); }}int main(){ while(~scanf("%d",&n)){ for(int i=0;i<=n;i++) f[i].clear(); memset(dp,0,sizeof dp); memset(vis,0,sizeof vis); for(int i=1;i<=n;i++){ scanf("%d:(%d)",&m,&k); while(k--){ scanf("%d",&x); f[m].push_back(x); f[x].push_back(m); } } dfs(0,-1); printf("%d\n",min(dp[0][0],dp[0][1])); }}
- 【贪心】【树形DP】[POJ1463][HDU1054]Strategic game 战略游戏
- ZOJ1134 POJ1463 HDU1054 Strategic Game, 树形DP
- HDU1054 && POJ1463:Strategic game(树形DP)
- poj1463--hdu1054--Strategic Game(树形DP练习4)
- HDU1054&&POJ1463-Strategic game
- POJ1463 Strategic game(树形DP)
- POJ1463 Strategic game(树形DP)
- poj1463 Strategic game 树形dp
- #POJ1463#Strategic Game(贪心 or Hungary or 树形DP)
- hdu1054 Strategic Game(贪心,树形dp,二分匹配)
- poj1463 Strategic game(树形dp)
- hdu1054 Strategic Game(最小覆盖点-树形dp)
- poj1463 Strategic game (树状dp)
- 【树形动态规划】poj1463 strategic game
- 树形dp Strategic game
- Strategic game(树形DP)
- hdu1054 Strategic Game —— 二分图最大匹配 or 树形dp
- poj1463 Strategic game
- Android Matrix对图片进行倾斜、缩放变换
- linux交叉编译问题
- leetcode之Find Minimum in Rotated Sorted Array II 问题
- Cannot connect to the Docker daemon at unix:///var/run/docker.sock.??
- web.xml中DispatcherServlet和ContextLoaderListener的区别
- 【贪心】【树形DP】[POJ1463][HDU1054]Strategic game 战略游戏
- 树莓派给特定USB端口特定设备绑定自定义设备符
- 【分治】Expanding Rods POJ 1905
- java的三种循环语句
- H265帧类型
- Java修改文件MD5值-yellowcong
- 操作项目中的资源文件-*.properties实例
- 1html
- 文章标题