Strategic game
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 5498   Accepted: 2484

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

本来感觉很简单的一个题,没想到竟然做了几个小时,又是初始化的问题,这次不是vector却是vis[],想哭的感觉。。。
Problem:  1463 User:  wust_******* Memory: 392K Time: 750MS Language: C++ Result:  Accepted
#include <iostream>
#include<vector>
#include<stdio.h>
using namespace std;
vector<int>s[1600];
int dp[2][1600],vis[1600],minn,f[1600]; int min(int i,int j)
{
return i<j?i:j;
} void dfs(int i)
{
dp[0][i]=0;
dp[1][i]=1;
int j;
if(s[i].size()==0)
return ;
for(j=0;j<s[i].size();j++)
{
if(vis[s[i][j]]==0)//WA多次后,有一次直接把这个标记的去掉竟然AC了,回过头来又检查发现是vis[]没初始化,修改之后再次提交发现时间没减少多少,我想可能是因为我从树的顶点开始dp
{
dfs(s[i][j]);
vis[s[i][j]]=1;
}
dp[0][i]+=dp[1][s[i][j]];//0表示无哨兵,i处无哨兵则s[i][j]处必须有哨兵
dp[1][i]+=min(dp[0][s[i][j]],dp[1][s[i][j]]);//1表示有哨兵,i处有哨兵的情况等于s[i][j]处有哨兵或者无哨兵的最小值
}
} int main()
{
int i,j,n,m,a,b,k,sum,leve;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
vis[i]=0;
s[i].clear();
}
for(i=0;i<=n;i++)
f[i]=i;
leve=0;
sum=0;
for(i=0;i<n;i++)
{
scanf("%d:(%d)",&a,&m);
sum+=a;//所有节点算总和
for(j=0;j<m;j++)
{
cin>>b;
s[a].push_back(b);
leve+=b;//算所有子树总和
}
}
k=sum-leve;//二者做差便是根节点
dfs(k);
minn=min(dp[0][k],dp[1][k]);//两种情况的最小值
cout <<minn<< endl;
}
return 0;
}


poj1463 Strategic game (树状dp)的更多相关文章

  1. 树状DP (poj 2342)

    题目:Anniversary party 题意:给出N各节点的快乐指数,以及父子关系,求最大快乐指数和(没人职员愿意跟直接上司一起玩): 思路:从底向上的树状DP: 第一种情况:第i个员工不参与,F[ ...

  2. poj3659树状DP

    Cell Phone Network Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6273   Accepted: 225 ...

  3. hdu 1561 The more, The Better_树状dp

    题目链接 题意:给你一棵树,各个节点都有价值(除根节点),从根节点出发,选择m个节点,问最多的价值是多小. 思路:很明显是树状dp,遍历树时背包最优价值,dp[i][k]=max{dp[i][r]+d ...

  4. poj 2342 Anniversary party_经典树状dp

    题意:Ural大学有n个职员,1~N编号,他们有从属关系,就是说他们关系就像一棵树,父节点就是子节点的直接上司,每个职员有一个快乐指数,现在要开会,职员和职员的直接上司不能同时开会,问怎才能使开会的快 ...

  5. 树状DP HDU1520 Anniversary party

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意:职员之间有上下级关系,每个职员有自己的happy值,越高在派对上就越能炒热气氛.但是必须是 ...

  6. [Codeforces743D][luogu CF743D]Chloe and pleasant prizes[树状DP入门][毒瘤数据]

    这个题的数据真的很毒瘤,身为一个交了8遍的蒟蒻的呐喊(嘤嘤嘤) 个人认为作为一个树状DP的入门题十分合适,同时建议做完这个题之后再去做一下这个题 选课 同时在这里挂一个选取节点型树形DP的状态转移方程 ...

  7. HDU 4714 Tree2cycle(树状DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup)

    Description A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 ...

  8. poj2486--Apple Tree(树状dp)

    Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7789   Accepted: 2606 Descri ...

  9. 洛谷P2015 二叉苹果树(树状dp)

    题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来 ...

随机推荐

  1. 网页动态切换母版页(MasterPage)

    原文:网页动态切换母版页(MasterPage) 是否可以变更网页的母版页(MasterPage)呢?某.aspx在创建时,已经附加入某一母版页(MasterPage)了,现需要.aspx动态变更母版 ...

  2. Linux之ftp命令使用

    一:前言 在达内參加暑期社会实践,达内公司免费教授了一星期的课,当时认为老师用ftp命令用的非常爽.所以回来学学了. 二:分类 有关FTP(client,server搭建这里不讲)有非常多,大体分为命 ...

  3. windows下 composer常见问题及处理

    错误一: Warning: This development build of composer is over 30 days old. It is recommend ed to update i ...

  4. userAgent,JS这么屌的用户代理,你造吗?——判断浏览器内核、浏览器、浏览器平台、windows操作系统版本、移动设备、游戏系统

    1.识别浏览器呈现引擎 为了不在全局作用域中添加多余变量,这里使用单例模式(什么是单例模式?)来封装检测脚本.检测脚本的基本代码如下所示: var client = function() { var ...

  5. Visual Studio 单元测试之六---UI界面测试

    原文:Visual Studio 单元测试之六---UI界面测试 UI界面测试其实就是录制操作路径(Mapping),然后按照路径还原操作顺序的一个过程.这个方法对于Winform和Webform都同 ...

  6. GIMP也疯狂之动态图的制作(三)

    Note:本篇文章内容为linux下利用gimp和其它工具组合完成截取制作GIF图的一种实现,制作更为灵活,但并不方便,如果不需要那么高的定制性,并追求方便高效.可以使用类似QQ影音之类的软件,自带截 ...

  7. ASP.NET-FineUI开发

    ASP.NET-FineUI开发 随笔分类 - FineUI   ASP.NET-FineUI开发实践-10 摘要: 嵌套Grid,光棍月大放送,不藏着掖着.实在写的不好,没脸藏啊~只考虑显示排序修改 ...

  8. Knockout简单用法

    Knockout简单用法 在最近做的一个项目中,页面数据全部通过js ajax调用webapi接口获取,也就是说页面的数据全部使用javascript脚本填充,这就想到了使用一个MVVM模式的js框架 ...

  9. Mysql高级之索引

    原文:Mysql高级之索引 索引:是针对数据所建立的目录. 作用: 可以加快查询速度 负面影响: 降低了增删改的速度. 索引的创建原则: 1:不要过度索引 2:在where条件最频繁的列上加.在重复度 ...

  10. .net图片快速去底(去除白色背景)

    public System.Drawing.Bitmap KnockOutGzf(String path) { System.Drawing.Image image = System.Drawing. ...