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. iOS 后台运行执行代码(例如定位)

  2. Spring - IoC(8): 基于 Annotation 的配置

    除了基于 XML 的配置外,Spring 也支持基于 Annotation 的配置.Spring 提供以下介个 Annotation 来标注 Spring Bean: @Component:标注一个普 ...

  3. BZOJ1082_栅栏_C++

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1082 题解:http://www.cnblogs.com/hadilo/p/5924546.h ...

  4. [bzoj1208][HNOI2004]宠物收养所——splay

    题目大意 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发 ...

  5. C# MVC 页面面包屑以及相应的权限验证操作

    一.特性类 /// <summary> /// 访问权限控制属性. /// </summary> [AttributeUsage(AttributeTargets.Method ...

  6. 培训补坑(day7:线段树的区间修改与运用)(day6是测试,测试题解以后补坑QAQ)

    补坑咯~ 今天围绕的是一个神奇的数据结构:线段树.(感觉叫做区间树也挺科学的.) 线段树,顾名思义就是用来查找一段区间内的最大值,最小值,区间和等等元素. 那么这个线段树有什么优势呢? 比如我们要多次 ...

  7. [转载]Go的50度灰:Golang新开发者要注意的陷阱和常见错误

    初级 开大括号不能放在单独的一行 未使用的变量 未使用的Imports 简式的变量声明仅可以在函数内部使用 使用简式声明重复声明变量 偶然的变量隐藏Accidental Variable Shadow ...

  8. 简单粗暴的webapp语言国际化

    不同语言以json格式存放不同文件 { "information": "个人资料", "fuckworld":"你好世界" ...

  9. ubuntu下wireshark+scapy+tcpreply

    安装wireshark命令: sudo apt-get install wireshark 运行打开wireshark命令: sudo wireshark(一定要以超级权限打开) 正确打开的窗口应该默 ...

  10. springmvc对于前台date类型注意点

    springmvc,可以自动将数据注入到: “name”值相同,便注入,比如String Integer 还有我们自定义的bean,比如User. 但是date类型的数据,如果前台传的是用" ...