Strategic game
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 9582   Accepted: 4516

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

题意:

给定一棵n个节点的树,如果在一个节点上放上一个士兵,所有与这个节点相连的边都可以被看守。现在希望所有的边都可以被看守,问需要最少放多少士兵。

思路:

比较典型的一个树形DP,我们用dp[i]表示以i为根的子树的最少士兵数。但是我们并不知道i上有没有士兵,转移方程就写不出来。

所以我们给dp再加一维,dp[i][1]表示以i为根并且i上有士兵的子树的最少士兵数,dp[i][0]为i上没有士兵。

那么对于某个节点rt,假设他的所有孩子的dp均已得到。

那么dp[rt][0] = dp[son][1]之和,因为他的每一个孩子都要有一个士兵。

dp[rt][1] = min(dp[son][0], dp[son][1])之和,即他的每一个孩子可以放士兵也可以不放士兵。

随便取一个节点作为树根,最后输出这个节点dp[rt][0],dp[rt][1]的较小值即可。

 //#include <bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<map>
#include<set> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; int n;
const int maxn = ;
vector<int>edge[maxn];
int dp[maxn][]; void dfsdp(int rt, int fa)
{
dp[rt][] = ;
dp[rt][] = ;
for(int i = ; i < edge[rt].size(); i++){
int son = edge[rt][i];
if(son == fa)continue;
else dfsdp(son, rt);
dp[rt][] += dp[son][];
dp[rt][] += min(dp[son][], dp[son][]);
} } int main(){ while(scanf("%d", &n) != EOF){
for(int i = ; i <= n; i++){
edge[i].clear();
}
for(int i = ; i <= n; i++){
int u, num;
scanf("%d:(%d)", &u, &num);
for(int j = ; j <= num; j++){
int v;
scanf(" %d", &v);
edge[u + ].push_back(v + );
edge[v + ].push_back(u + );
}
} dfsdp(, );
printf("%d\n", min(dp[][], dp[][]));
}
return ;
}

poj1463 Strategic game【树形DP】的更多相关文章

  1. poj1463 Strategic game[树形DP]

    求一棵树每条边都被选上的点覆盖掉的最少选点数. 一条边被覆盖掉,必须他父亲和儿子中选一个..这不就是比NOIP2018D2T3还裸的暴力么.水掉. lyd给的练习题都什么**玩意儿.. code不挂了 ...

  2. POJ1463:Strategic game(树形DP)

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

  3. Strategic game树形DP解法(Poj1463,Uva1292)

    已经写过本题用二分图的做法,见这儿. 本题的图是一棵树,求最小点覆盖也可以用树形DP的做法. 定义状态f[0/1][u]表示以u为根的子树,u选取/不选最少需要选取多少点来覆盖. 显然 f[0][u] ...

  4. HDU 1054 Strategic Game (树形dp)

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

  5. UVa 1292 - Strategic game (树形dp)

    本文出自   http://blog.csdn.net/shuangde800 题目链接: 点击打开链接 题目大意 给定一棵树,选择尽量少的节点,使得每个没有选中的结点至少和一个已选结点相邻. 思路 ...

  6. hdu1054 Strategic Game 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 思路:树形DP,用二分匹配也能解决 定义dp[root][1],表示以root 为根结点的子树且 ...

  7. POJ 1463 Strategic game(树形DP入门)

    题意: 给定一棵树, 问最少要占据多少个点才能守护所有边 分析: 树形DP枚举每个点放与不放 树形DP: #include<cstdio> #include<iostream> ...

  8. Strategic game(树形DP入门)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 题目大意:一棵树,要放置哨兵,要求最少放置多少哨兵能监视到所有的结点 题目分析: 放置哨兵无非两 ...

  9. Strategic game(POJ 1463 树形DP)

    Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 7490   Accepted: 3483 De ...

随机推荐

  1. am335x 10.1"电容touch 不能识别

    /**************************************************************** * am335x 10.1"电容touch 不能识别 * ...

  2. 下列没有直接采用XML技术的是( )

    A. UDDI B. SOAP C. AJAX D.DCOM 解答:D DCOM(分布式组件对象模型,分布式组件对象模式)是一系列微软的概念和程序接口,利用这个接口,客户端程序对象能够请求来自网络中另 ...

  3. 关于document.createDocumentFragment()(转)

    documentFragment 是一个无父对象的document对象. 他支持以下DOM2方法: appendChild, cloneNode, hasAttributes, hasChildNod ...

  4. linux下常用FTP命令 1. 连接ftp服务器

    1. 连接ftp服务器 格式:ftp [hostname| ip-address] a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密 ...

  5. TTreeView TTreeNodes TTreeNode

    TTreeView 填写 TTreeView 的内容一般是这样开始的(下图), 不过我觉得最好习惯用动态建立. 打个比方: 譬如 TreeView 是一个军营的"营部"! 这里会有 ...

  6. string 线程安全

    线程安全:class Program { public delegate void MyDelegate(string aa); static void Main(string[] args) { M ...

  7. 计算从ios照片库中选取的图片文件大小

    本文转载至:http://blog.csdn.net/longzs/article/details/8373586 从 iphone 的 照片库中选取的图片,由于 系统不能返回其文件的具体路径,所以这 ...

  8. (转)关于android设备管理器的一些分析

    转自http://bbs.pediy.com/showthread.php?t=183692 想必很多人都知道轰动一时android木马OBAD,该木马利用android设备管理器的漏洞,当用户激活设 ...

  9. 设计模式之装饰模式(Java实现)

    “怎么了,鱼哥?” “唉,别提了,网购了一件衣服,结果发现和商家描述的差太多了,有色差就算了,质量还不好,质量不好就算了,竟然大小也不行,说好的3个X,邮的却是一个X的,不说了,退货去.你先开讲吧,你 ...

  10. 封装JDBC工具类

    JDBC连接数据库基本的步骤是固定的,这样就可以考虑封装一个工具类来简化数据库操作. 封装时用到了Java中的properties配置文件,是以一种键值对的形式存在的,可以把连接数据库要动态的信息保存 ...