HDU 1054 Strategic Game(树形DP)
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:
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一般的树形DP;
2#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<vector>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
vector<int>v[1500];
int vis[1500];
int dp[1500][2];
int n,m;
void dfs(int x)
{
if(vis[x]) return ;
vis[x]=1;
dp[x][1]=1;
dp[x][0]=0;
for(int i=0;i<v[x].size();i++)
{
if(!vis[v[x][i]])
{
dfs(v[x][i]);
dp[x][0]+=dp[v[x][i]][1];
dp[x][1]+=min(dp[v[x][i]][1],dp[v[x][i]][0]);
}
}
}
int main()
{
int x,y,temp;
while(~scanf("%d",&n))
{
REP(i,n)
v[i].clear();
REPF(i,1,n)
{
scanf("%d:(%d)",&x,&y);
while(y--)
{
scanf("%d",&temp);
v[x].push_back(temp);
v[temp].push_back(x);
}
}
CLEAR(vis,0);
dfs(1);//因为是无向图能够在0~n中随变找一个点
printf("%d\n",min(dp[1][1],dp[1][0]));
}
return 0;
}
HDU 1054 Strategic Game(树形DP)的更多相关文章
- HDU 1054 Strategic Game (树形dp)
题目链接 题意: 给一颗树,用最少的点覆盖整棵树. 每一个结点可以防守相邻的一个边,求最少的点防守所有的边. 分析: 1:以当前节点为根节点,在该节点排士兵守护道路的最小消耗.在这种情况下,他的子节点 ...
- hdu 1054 Strategic Game(tree dp)
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu1054 Strategic Game 树形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 思路:树形DP,用二分匹配也能解决 定义dp[root][1],表示以root 为根结点的子树且 ...
- HDU - 1054 Strategic Game(二分图最小点覆盖/树形dp)
d.一颗树,选最少的点覆盖所有边 s. 1.可以转成二分图的最小点覆盖来做.不过转换后要把匹配数除以2,这个待细看. 2.也可以用树形dp c.匈牙利算法(邻接表,用vector实现): /* 用ST ...
- hdu 1054 Strategic Game (简单树形DP)
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1054 Strategic Game(最小点覆盖+树形dp)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=106048#problem/B 题意:给出一些点相连,找出最小的点数覆盖所有的 ...
- Strategic game(树形DP入门)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 题目大意:一棵树,要放置哨兵,要求最少放置多少哨兵能监视到所有的结点 题目分析: 放置哨兵无非两 ...
- POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题
一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...
- POJ1463:Strategic game(树形DP)
Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot f ...
随机推荐
- NYOJ 12 喷水装置(二)
pid=12">喷水装置(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n( ...
- 安装 MYSQL exec: g++: not found 报错
解决办法: yum install -y gcc-c++
- Hdu 4539 【状态DP】.cpp
题意: 一个炮兵可以攻打和他之间曼哈顿距离为2的士兵,给出你一块n*m的战场,告诉你哪些地方可以站人哪些地方不可以,问你最多可以安放多少个士兵? n <= 100, m <= 10 思路: ...
- 使用OGG"Loading data from file to Replicat"的方法应该注意的问题:replicat进程是前台进程
使用OGG的 "Loading data from file to Replicat"的方法应该注意的问题:replicat进程是前台进程 因此.最好是在vncserver中调用该 ...
- Objective-C NSObject 的实现分析(2014-10-23更新)
NSObject 的实现分析 转载请注名出处 http://blog.csdn.net/uxyheaven iOS 的 NSObject 类没有开源, 可是呢 runtime开源了,里面有个类 Obj ...
- cocos2d-x 3.2 它 三消游戏——万圣节大作战
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- (23)unity4.6学习Ugui中国文档-------非官方Demo1
大家好,我是广东太阳. 转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:http://www.unitym ...
- Windows Phone开发(27):隔离存储A
原文:Windows Phone开发(27):隔离存储A 在很多资料或书籍上都翻译为"独立存储",不过,我想了一下,决定将IsolatedStorage翻译为"隔离存储& ...
- 一个Jquery特效(转)
见证花开!!码上有花 先看效果 尝试点击,IE9+ 画心 清空 ... 请自由点击画布 创意来自于网络 可搜搜[程序员表白] 看上去像是[当耐特]的作品的加工版本 代码 当然要自己写了 能力有限, ...
- JAVA: httpclient 具体解释——第五章;
httpclient 具体解释--第一章: httpclient 具体解释--第二章: httpclient 具体解释--第三章: httpclient 具体解释--第四章: httpclient 具 ...