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. DOM读取和修改节点对象属性

    一.获取和修改元素间的内容(3种) 1.innerHTML 获得/设置元素开始标签和结束标签之间的html原文 固定套路:1.删除父元素下所有子元素:parent.innerHTML="&q ...

  2. 02-导航实例-storyboard实现

        源代码下载链接:02-导航实例-storyboard实现.zip38.5 KB // MJAboutViewController.h // //  MJAboutViewController. ...

  3. linux基础 -nginx和nfs代理 开发脚本自动部署及监控

    开发脚本自动部署及监控 1.编写脚本自动部署反向代理.web.nfs: (1).部署nginx反向代理三个web服务,调度算法使用加权轮询:  (2).所有web服务使用共享存储nfs,保证所有web ...

  4. (二十八)fopen与读写的标识r,r+,rb+,rt+,w+.....

    fopen与读写的标识r,r+,rb+,rt+,w+..... 函数简介 函数功能: 打开一个文件 函数原型:FILE * fopen(const char * path,const char * m ...

  5. js监听不到组合键

    我在js文件中写代码,监听 ctrl + enter 组合键,但是一直监听不到.只能监听到单个键. 后来我将监听的代码放到html页面中去,就能监听到了. 这个问题困扰我很久,记录下!

  6. python memcache操作-安装、连接memcache

    安装memecache wget http://memcached.org/latest tar -zxvf memcached-1.x.x.tar.gz cd memcached-1.x.x ./c ...

  7. flask+gunicorn中文文件下载报错问题及解决

    导言 问题源起与一个静态文件下载的接口: from flask import Flask, current_app app = Flask(__name__) @app.route('/file_na ...

  8. AJAX--前后台交互

    注:ajax通过async参数决定是异步还是同步,false同步,true异步; 异步执行顺序是先执行后续动作,再执行success里代码; 同步是先执行success里代码,再执行后续代码; 验证: ...

  9. 生产环境安装centos时的磁盘规划

    一般来说,分区要按照公司领导的要求来执行.但是如果没有要求,一般按照下面的方法进行磁盘规划. /boot分区200M: swap分区分内存的2倍.如果内存大于等于8G,那么swap分8G即可: /分区 ...

  10. redis使用中的常见错误

    1.2016年12月17日  启动redis报错,错误信息如下: 解决办法:redis没有正常关闭(redis安装在虚拟机上,直接杀死了虚拟机进程) 导致redis.pid文件一直被锁定,重启redi ...