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. linux 定时关机命令

    一. 关机流程 Linux 运作时, 不能够直接将电源关闭, 否则, 可能会损毁档案系统. 因此, 必须依照正常的程序关机: 观察系统使用情形(或许当时, 正有使用者做着重要的工作呢!) 通知线上使用 ...

  2. 为Pythonic论坛添加一个“专题”功能(续)

    上篇博文<为Pythonic论坛添加一个“专题”功能>,在模板的层次上对发帖进行了限制.也就是根据用户是否拥有权限来决定是否显示发帖框. 但是自从这么“投机取巧”的写完模板后,整夜辗转反侧 ...

  3. 让apache2不开机启动,管理Ubuntu的开机启动项

    今天在网上发现了一个很好用的管理Ubuntu下开关启动的软件,叫做sysv-rc-conf 使用命令行: tf@ubuntu:/etc/apache2$ sudo update-rc.d -f apa ...

  4. 安装Windows操作系统的驱动程序(驱动精灵版) - 进阶者系列 - 学习者系列文章

    安装完操作系统之后,就需要安装对应的驱动程序了.下面就简要介绍下驱动程序的安装.以驱动精灵驱动安装软件为例. 1.  下载驱动精灵. 从http://www.drivergenius.com/网站下载 ...

  5. js urlencode , encodeURIComponent

    js 对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent ...

  6. iOS基础 - UIDatePicker and UIPickerView and UITextField

    1.UIDatePicker继承自UIControl,因此不会通过代理来监听事件的改变,而是通过addTarget来监听事件.监听的事件是值改变事件. 2.UIPickerView继承自UIView, ...

  7. C# 脚本代码自动登录淘宝获取用户信息

    C# 脚本代码自动登录淘宝获取用户信息   最近遇到的一个需求是如何让程序自动登录淘宝, 获取用户名称等信息. 其实这个利用SS (SpiderStudio的简称) 实现起来非常简单. 十数行代码就可 ...

  8. HttpModule应用

    由做网站操作日志想到的HttpModule应用   背景 在以前的Web项目中,记录用户操作日志,总是在方法里,加一行代码,记录此时用户操作类型与相关信息.该记录日志的方法对原来的业务操作侵入性较强, ...

  9. 包图Package

    [UML]UML系列——包图Package 系列文章 [UML]UML系列——用例图Use Case       [UML]UML系列——用例图中的各种关系(include.extend)       ...

  10. CLR_Via_C#学习笔记之枚举

    CLR_Via_C#学习笔记之枚举 枚举类型(Enum)定义的一组"符号名称/值"配对:因为枚举类型使用程序更容易编写.阅读和维护,而且它是强类型: 枚举是值类型:由System. ...