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. 转载JQuery绑定鼠标粘贴事件工具类

    // 粘贴事件监控 $.fn.pasteEvents = function( delay ) { if (delay == undefined) delay = 10; return $(this). ...

  2. 一个意想不到的CDO.Message 错误

    原文:一个意想不到的CDO.Message 错误   几个月之前,写了一个服务从MSMQ取消息发群发邮件的程序,一直也没时间测试,今日一试,出现发送邮件时报错,异常情况如下:   "Syst ...

  3. 在weblogic11g发布该项目时遇到错误(不支持web-app_3_0)

    problem: cvc-enumeration-valid: string value '3.0' is not a valid enumeration value for web-app-vers ...

  4. Building Modern Web Apps-构建现代的 Web 应用程序

    Building Modern Web Apps-构建现代的 Web 应用程序 视频长度:1 小时左右 视频作者:Scott Hunter 和 Scott Hanselman 视频背景:Visual ...

  5. codeblock 设置背景颜色

    今天觉得codeblock看着刺眼,想了想,能不能跟vs利用dark背景一样可以设置一个舒服的背景.于是就开始各种点击,各种摸索了. 1. 背景和各种颜色设置 不妨先说下vs中的设置是:工具(Tool ...

  6. Windows下MYSQL自动备份批处理

    windows下MYSQL自动备份批处理 2011-05-04 09:16:45|  分类: mysql|举报|字号 订阅     按系统时间进行备份 注意mysql安装路径中如果有空格.就要把,.b ...

  7. Roslyn 编译平台概述

    在Language Feature Status上面看到,其实更新的并不是特别多,为了不会误导看了C# 6.0 功能预览 (一)的园友,现在把官方的更新列表拿了过来,供大家参考 C# 6.0 功能预览 ...

  8. 【推荐分享】大量Python电子书籍教程pdf合集下载

    网上搜集的,点击即可下载,希望提供给有需要的人^_^   O'Reilly.Python.And.XML.pdf 2.02 MB   OReilly - Programming Python 2nd. ...

  9. JAVA修饰符类型(转帖)

    JAVA修饰符类型(public,protected,private,friendly) public的类.类属变量及方法,包内及包外的任何类均可以访问:protected的类.类属变量及方法,包内的 ...

  10. Model Validation(模型验证)

    Model Validation(模型验证) 前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/344 ...