Strategic Game

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

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
 
Source
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 2010
int Min(int i,int j){return i<j?i:j;}
struct node{
node *l,*r;//l-son,r-brother
int f[2];
void init(){
l=r=NULL;f[0]=f[1]=0;
}
}t[N],*rt;
int r[N];
void dfs(node *x){
//遍历x儿子
node *l=x->l;
while(l){
dfs(l);
//放在x上
x->f[1]+=Min(l->f[0],l->f[1]);
//x不放
x->f[0]+=l->f[1];
l=l->r;
}
}
int main(){
int i,j,k,x,y,n,m;
while(~scanf("%d",&n)){
m=0;
memset(r,-1,sizeof(r));
while(n--){
scanf("%d:(%d)",&i,&k);
if(r[i]==-1){r[i]=m;t[m++].init();}
x=r[i];
while(k--){//i->l=j
scanf("%d",&j);
if(r[j]==-1){r[j]=m;t[m++].init();}
y=r[j];
t[y].f[1]=1;
t[y].r=t[x].l;
t[x].l=&t[y];
}
}
for(i=0;;i++)
if(t[r[i]].f[1]==0){
rt=&t[r[i]];
rt->f[1]=1;
break;
}
dfs(rt);
printf("%d\n",Min(rt->f[0],rt->f[1]));
}
return 0;
}

hdu 1054 Strategic Game(tree dp)的更多相关文章

  1. HDU 1054 Strategic Game (树形dp)

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

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

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

  3. HDU 1054 Strategic Game(树形DP)

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

  4. hdu 1054 Strategic Game 经典树形DP

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

  5. hdu 1054 Strategic Game (简单树形DP)

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

  6. HDU 1054 Strategic Game(树形DP)

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

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

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

  8. HDU 1054:Strategic Game

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

  9. HDU 5629 Clarke and tree dp+prufer序列

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=562 题意: 求给每个节点的度数允许的最大值,让你求k个节点能组成的不同的生成树个数. 题解: 对于n ...

随机推荐

  1. java获取mysql数据库表、字段、字段类型、字段注释

    最近想要写一个根据数据库表结构生成实体.mapper接口.mapping映射文件.service类的简单代码生成工具,所以查阅了一些资料,怎样获取数据库的表.表中字段.字段类型.字段注释等信息. 最后 ...

  2. webpack3基础知识

    ## 本地化安装webpack ## 1. npm init //npm初始化生成package.json文件 2. npm install --save-dev webpack //安装webpac ...

  3. 复选框 checkbox 选中事件

    项目中用的jquery-1.11 今天需要检测一个checkbox的选中状态,想当然的用 .attr("checked") ,结果发现,无论是否选中,这个值都是 undefined ...

  4. ILSPY反编译工具下载代替收费的Reflector工具

    原文发布时间为:2011-10-10 -- 来源于本人的百度文章 [由搬家工具导入] ILSPY反编译工具下载 http://build.sharpdevelop.net/BuildArtefacts ...

  5. <script>中的async与defer属性

    1.script元素中的defer属性 1.1说明 使用该属性可以使脚本延迟到文档完全被解析和显示之后再按照原本的顺序执行,即告诉浏览器立即下载脚本,但延迟执行,该属性只对外部脚本有效 1.2使用方法 ...

  6. python encode和decode函数说明

    字符串编码常用类型:utf-8,gb2312,cp936,gbk等. Python中,我们使用decode()和encode()来进行解码和编码 在python中,使用unicode类型作为编码的基础 ...

  7. eps图片中有中文乱码的问题

    一般的,如果matlab中的fig图片中有中文,直接saveas为eps,eps再插入latex后会出现乱码. 解决的办法为: (1) *.fig利用‘file--print’保存为*.pdf (2) ...

  8. JSON格式数据的js操作

    第一种方式: 使用js函数eval(); testJson=eval(testJson);是错误的转换方式. 正确的转换方式需要加(): testJson = eval("(" + ...

  9. (九)MySQL用户和权限管理

    (1)用户管理 1)登录和退出mysql 例: mysql -h192.168.111.150 -P3306 -uroot -predhat mysql -e 'select user,host,au ...

  10. [jquery] 删除文章的时候弹出确认窗口

    [<a href="{:U(GROUP_NAME . '/Category/delCate')}/id/{$v.id}" onclick='return del();'> ...