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. sqlserver检测数据库是否能连接的小技巧

    有时候可能需要检测下某台机器的服务是不是起来了,或者某台机器的某个库是不是能被连接又不能打开ssms也不想登陆服务器的话就可以用这个方法. 1.在桌面上右键创建个文本,然后改后缀名为udl以后保存(1 ...

  2. 浅谈javascript性能-管理内存

    上次说到,javascript脚本到底应该放在哪里?用什么用处? 以下2点: 在Html.Body部分中的JS会在页面加载的时候执行.即-用户触发一个事件的时候执行的脚本.eg:onload事件... ...

  3. C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 | IT宅.com

    原文:C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 | IT宅.com C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 | I ...

  4. 编译预处理 -- 带参数的宏定义--【sky原创】

    原文:编译预处理 -- 带参数的宏定义--[sky原创] 如有转载请注明出处   编译预处理  --  带参数的宏定义 前面为输出文件,后面为输入文件 gcc -E -o test.i test.c ...

  5. angular.js的一点理解

    对angular.js的一点理解 2015-01-14 13:18 by MrGeorgeZhao, 317 阅读, 4 评论, 收藏, 编辑 最近一直在学习angular.js.不得不说和jquer ...

  6. FlexiGrid使用手册

    FlexiGrid使用手册 一.概览 Flexigrid是一个基于jQuery开发的Grid,与 Ext Gird类似.Flexigrid显示的数据能够通过Ajax获取或者从一个普通的表格转换. 它的 ...

  7. webkit内核下的mouseup后mousemove自动触发问题及解决方法

    如题,就以chrome为代表举例说明遇到mousemove的问题. 为body分别绑定onmousedown.onmousemove.onmouseup,并为触发时打印至控制台.代码如下(同学不要忘记 ...

  8. sql点滴38—SQL Server 2008和SQL Server 2008 R2导出数据的选项略有不同

    原文:sql点滴38—SQL Server 2008和SQL Server 2008 R2导出数据的选项略有不同 说明: 以前要将一个表中的数据导出为脚本,只有用存储过程.现在在SQL Server ...

  9. 用一条SQL语句取出第 m 条到第 n 条记录的方法

    原文:用一条SQL语句取出第 m 条到第 n 条记录的方法   --从Table 表中取出第 m 条到第 n 条的记录:(Not In 版本)       *    FROM Table     id ...

  10. 5款新颖的ReSharper插件

    ReSharper是著名的代码生成工具.自ReSharper 8.0版本发布以来,新的扩展管理器.模板设置.分发安装等功能改革使得对插件的要求变高.接下来小编整理了5款新颖插件. JetBox 当开发 ...