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

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

一道简单的树形DP,状态转移方程:和上一次做的题目基本一样
  dp[node][0]+=dp[tree[node][i]][1];

  dp[node][1]+=min(dp[tree[node][i]][0],dp[tree[node][i]][1]);  然后+1

一开始写成 dp[node][1]+=dp[tree[node][i]][0];

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define Max 1506
int dp[Max][],fa[Max],tree[Max][];
int n;
int num[Max];
void tree_dp(int node)
{
int i;
for(i=;i<num[node];i++)
{
tree_dp(tree[node][i]);
dp[node][]+=dp[tree[node][i]][];
dp[node][]+=min(dp[tree[node][i]][],dp[tree[node][i]][]);
}
dp[node][]++;
return;
}
int main()
{
int i,j;
int a,b;
int k,root,f;
freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
memset(dp,,sizeof(dp));
memset(tree,,sizeof(tree));
for(i=;i<Max;i++)
fa[i]=-;
for(i=;i<n;i++)
{
scanf("%d%*c%*c%d%*c",&f,&k);
num[f]=k;
for(j=;j<k;j++)
{
scanf("%d",&tree[f][j]);
fa[tree[f][j]]=f;
}
}
root=;
while(fa[root]!=-)
root=fa[root];
tree_dp(root);
printf("%d\n",min(dp[root][],dp[root][]));
}
return ;
}

Strategic game(POJ 1463 树形DP)的更多相关文章

  1. poj 1463(树形dp)

    题目链接:http://poj.org/problem?id=1463 思路:简单树形dp,如果不选父亲节点,则他的所有的儿子节点都必须选,如果选择了父亲节点,则儿子节点可选,可不选,取较小者. #i ...

  2. (探讨贴)POJ 1463 树形DP解法的不正确性

    POJ1463是一个典型的树状DP题. 通常解法如下代码所示: using namespace std; ; ]; int pre[maxn]; int childcnt[maxn]; int n; ...

  3. poj 1463树形dp 树的最小覆盖

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...

  4. 树形dp compare E - Cell Phone Network POJ - 3659 B - Strategic game POJ - 1463

    B - Strategic game POJ - 1463   题目大意:给你一棵树,让你放最少的东西来覆盖所有的边   这个题目之前写过,就是一个简单的树形dp的板题,因为这个每一个节点都需要挺好处 ...

  5. Fire (poj 2152 树形dp)

    Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...

  6. poj 2486( 树形dp)

    题目链接:http://poj.org/problem?id=2486 思路:经典的树形dp,想了好久的状态转移.dp[i][j][0]表示从i出发走了j步最后没有回到i,dp[i][j][1]表示从 ...

  7. poj 3140(树形dp)

    题目链接:http://poj.org/problem?id=3140 思路:简单树形dp题,dp[u]表示以u为根的子树的人数和. #include<iostream> #include ...

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

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

  9. POJ 2342 树形DP入门题

    有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...

随机推荐

  1. 64位Ubuntu14.04搭建ADT开发环境

    本来放假是要出去玩的,结果出游计划拖到了周一,今天先来无事看看ubuntu最近发展得怎么样了,顺便把ADT也搭建好,方便以后研究android框架.想想在windows下,ADT直接down下来解压就 ...

  2. s3c2416裸跑环境配置

    最近刚刚开始学习ARM-linux,上周买了块tq2416的板子,给的Linux资料太复杂太深奥不愿看,等不及想要把2416跑起来.于是到处找相关裸跑资料,可是用2416的人实在少,网上的资料更少,裸 ...

  3. Windows 8.1 with Update 镜像下载(增OEM单语言版)

    该系统已有更新的版本,请转至<Windows 8.1 with update 官方最新镜像汇总>下载. 2014年4月9日凌晨,微软向MSDN订阅用户开放了Windows 8.1 with ...

  4. ./scripts/feeds update -a OpenWrt大招系列

    ./scripts/feeds update -a Updating feed 'packages' from 'https://github.com/openwrt/packages.git' .. ...

  5. Thinkphp 3.0版本上传文件加图片缩略图实例解析

    先看html加个表单,注意这里的action 路径要选 对. <div> <form action="__URL__/add_img" enctype=" ...

  6. iOS 面试常见问题最全梳理

    序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了.今年,找过工作人可能会更深刻地体会到今年的就业形势不容乐观,加之,培训机构一火车地向用人单位输送iOS开发人员,打破了生态圈的动态 ...

  7. Linux环境 Mysql新建用户和数据库并授权

    测试环境:linux 和Mysql 5.5.35 一.新建用户 //登录Mysql@>mysql -u root -p@>密码//创建用户mysql> insert into mys ...

  8. 在Struts2中使用Uploadify组件上传文件

    Uploadify是一个基于Jquery的文件上传组件,官网http://www.uploadify.com/可以在官网获得该组件,运行演示示例,下载帮助文档.     作为Web前端的增强技术,Jq ...

  9. 再一次强调,ORACLE外键必须加索引

    外键加索引是常识,必须牢记.本来不想写这样的简单案例.可是连续遇到好几起外键不加索引导致性能问题,所以还是写一下. 一个兄弟问我 delete from Sa_Sales_Comm_Detail s  ...

  10. 不用position,让div垂直居中

    先弄懂after伪类的用法,就可以很容易理解了. <!DOCTYPE html> <html lang="en"><head> <meta ...