POJ1463-Strategic game
来源:互联网 发布:网络品牌策划 编辑:程序博客网 时间:2024/05/16 17:12
Strategic game
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 8433 Accepted: 3945
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
Source
Southeastern Europe 2000
题目大意: 给出一个树,在一些结点放士兵,要监视所有点边,问最少放多少士兵。
解题思路:树形dp,
对于叶子结点,
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int MAXN=1e5+5;int head[MAXN],tot;int dp[MAXN][2];struct Edge{ int from,to,nxt;}e[MAXN*2];void addedge(int u,int v){ e[tot].from=u; e[tot].to=v; e[tot].nxt=head[u]; head[u]=tot++;}void dfs(int rt,int fa){ dp[rt][0]=0;dp[rt][1]=1; for(int i=head[rt];i!=-1;i=e[i].nxt) { int to=e[i].to; if(to==fa) return; dfs(to,rt); dp[rt][0]+=dp[to][1]; dp[rt][1]+=min(dp[to][0],dp[to][1]); }}int main(){ int n; while(scanf("%d",&n)!=EOF) { tot=0; memset(head,-1,sizeof(head)); memset(dp,0,sizeof(dp)); int root=-1; for(int i=1;i<=n;++i) { int u,v,m; scanf("%d:(%d)",&u,&m); if(root==-1&&m!=0) root=u; for(int j=1;j<=m;++j) { scanf("%d",&v); addedge(u,v); addedge(v,u); } } dfs(root,-1); printf("%d\n",min(dp[root][1],dp[root][0])); } return 0;}
- poj1463 Strategic game
- poj1463 Strategic game
- HDU1054&&POJ1463-Strategic game
- POJ1463-Strategic game
- poj1463——Strategic game
- poj1463 Strategic game (树状dp)
- POJ1463 Strategic game(树形DP)
- POJ1463 Strategic game(树形DP)
- poj1463 Strategic game 树形dp
- poj1463——strategic game
- ZOJ1134 POJ1463 HDU1054 Strategic Game, 树形DP
- 【树形动态规划】poj1463 strategic game
- HDU1054 && POJ1463:Strategic game(树形DP)
- poj1463 Strategic game(树形dp)
- poj1463--hdu1054--Strategic Game(树形DP练习4)
- 分治法或拓扑排序 poj1463 Strategic game
- #POJ1463#Strategic Game(贪心 or Hungary or 树形DP)
- 【贪心】【树形DP】[POJ1463][HDU1054]Strategic game 战略游戏
- 安卓动画(旋转,平移,透明,缩放,帧动画)
- LCS 最大公共序列算法
- 数据库里的DM5和SHA1
- jsp九大内置对象
- [Leetcode] 339. Nested List Weight Sum 解题报告
- POJ1463-Strategic game
- svn clean up falied 终极解决方案
- 居民身份证号码的编码规则
- HTML常用的特殊符号总结 2014年9月12日 22704次浏览 html中经常会用到一些特殊符号,例如箭头,雪花,心形等等,这些符号就不用css样式或者图片来写了,直接用html特殊符号可以实现。
- Android Studio 3.+版本 Gradle中Complie和Implementation
- CodeM 数码
- 二叉树相关操作(前序遍历,中序遍历,后序遍历,层次序遍历等)递归和非递归实现
- 不带权并查集的操作
- ILRuntime第二课预备知识-ILRuntime中的反射