B - Strategic Game

Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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.

The input file 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_identifier
or
node_identifier:(0)

The node identifiers are integer numbers between 0 and
n-1, for n nodes (0 < n <= 1500). Every edge appears only once in
the input data.

For example for the tree:

the solution is one soldier ( at the node 1).

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 table:

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
前几天刚做过这题,二分图A的,树形dp同样可以解决
注意    此题要求任意一条路径上都应当有人看守,所有dp[root][0]+=dp[k][1],,,此处0的时候只能选着儿子节点的1
 
dp[root][1]=min(dp[k][0],dp[k][1])
题很水不多说了
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
struct node{
int to,net;
}edge[maxn<<]; int head[maxn];
bool vis[maxn];
int dp[maxn][];
int tot; void addedge(int u,int v){ edge[tot].to=v;
edge[tot].net=head[u];
head[u]=tot++; edge[tot].to=u;
edge[tot].net=head[v];
head[v]=tot++; } void dfs(int root){
vis[root]=true; for(int i=head[root];i!=-;i=edge[i].net){
int v=edge[i].to;
if(!vis[v]){
dfs(v);
dp[root][]+=dp[v][];
dp[root][]+=min(dp[v][],dp[v][]); }
}
} int main(){
int t;
while(scanf("%d",&t)!=EOF){
memset(dp,,sizeof(dp));
memset(vis,false,sizeof(vis));
tot=;
memset(head,-,sizeof(head));
for(int i=;i<=t;i++){
int n,x,y;
scanf("%d:(%d)",&x,&n);
for(int j=;j<=n;j++){
scanf("%d",&y);
addedge(x,y);
}
}
int root=;
for(int i=;i<t;i++)
dp[i][]=;
dfs(root);
int ans=min(dp[root][],dp[root][]);
printf("%d\n",ans);
}
return ;
}
 

hdu1054 树形dp&&二分图的更多相关文章

  1. HDU - 1054 Strategic Game(二分图最小点覆盖/树形dp)

    d.一颗树,选最少的点覆盖所有边 s. 1.可以转成二分图的最小点覆盖来做.不过转换后要把匹配数除以2,这个待细看. 2.也可以用树形dp c.匈牙利算法(邻接表,用vector实现): /* 用ST ...

  2. HDU1054 Strategic Game —— 最小点覆盖 or 树形DP

    题目链接:https://vjudge.net/problem/HDU-1054 Strategic Game Time Limit: 20000/10000 MS (Java/Others)     ...

  3. HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP

    分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...

  4. hdu1054 Strategic Game 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 思路:树形DP,用二分匹配也能解决 定义dp[root][1],表示以root 为根结点的子树且 ...

  5. hdu1520 Anniversary party 简单树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 思路:树形DP的入门题 定义dp[root][1]表示以root为根节点的子树,且root本身参 ...

  6. 洛谷AT2046 Namori(思维,基环树,树形DP)

    洛谷题目传送门 神仙思维题还是要写点东西才好. 树 每次操作把相邻且同色的点反色,直接这样思考会发现状态有很强的后效性,没办法考虑转移. 因为树是二分图,所以我们转化模型:在树的奇数层的所有点上都有一 ...

  7. 树形DP小结

    树形DP1.简介:树是一种数据结构,因为树具有良好的子结构,而恰好DP是从最优子问题更新而来,那么在树上做DP操作就是从树的根节点开始深搜(也就是记忆化搜索),保存每一步的最优结果.tips:树的遍历 ...

  8. SPOJ 1479 +SPOJ 666 无向树最小点覆盖 ,第二题要方案数,树形dp

    题意:求一颗无向树的最小点覆盖. 本来一看是最小点覆盖,直接一下敲了二分图求最小割,TLE. 树形DP,叫的这么玄乎,本来是线性DP是线上往前\后推,而树形DP就是在树上,由叶子结点状态向根状态推. ...

  9. Luogu P2458 [SDOI2006]保安站岗【树形Dp】

    题目描述 五一来临,某地下超市为了便于疏通和指挥密集的人员和车辆,以免造成超市内的混乱和拥挤,准备临时从外单位调用部分保安来维持交通秩序. 已知整个地下超市的所有通道呈一棵树的形状:某些通道之间可以互 ...

随机推荐

  1. PHP_php.ini_说明详解

    这个文件必须命名为''php.ini''并放置在httpd.conf中的PHPIniDir指令指定的目录中.最新版本的php.ini可以在下面两个位置查看:http://cvs.php.net/vie ...

  2. OSX10.11 删除系统自带的软件

    之前一直用sudo rm - rf 系统的浏览器名字 正常删除safari 升级到10.11后,完全没作用了 需要关闭系统的什么安全模式 csrutil disable 再进入系统使用此命令可正常删除 ...

  3. js变量在属性里的写法 常用mark 多个DL遍历添加一个父级DIV

    标记用 js变量比如url链接一般都是a里面的href属性值 在js里单引号链接 以后再忘记就能有地方找了 例子: /* 添加1200 居中div 包裹 获取元素集合 上层元素100% * @ele ...

  4. linux程序调试命令strace

    strace命令用法详解: strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须 ...

  5. Android中Java与JavaScript之间交互(转)

    Android代码: package com.fyfeng.testjavascript; import android.app.Activity; import android.content.In ...

  6. jQuery中$(function(){})与(function($){})(jQuery)、$(document).ready(function(){})等的区别详细讲解

    1.(function($) {…})(jQuery); 1).原理: 这实际上是匿名函数,如下: function(arg){…} 这就定义了一个匿名函数,参数为arg 而调用函数时,是在函数后面写 ...

  7. Python 文件遍历

    Python具备强大的解析能力,其中列表解析甚至可以作用在某些并非实际存储的序列上,任何可遍历对象都可以,包括可自动逐步读取的文件. 例如下面的代码将会从逐行读取一个文本文件,并且在每一行的最后加上一 ...

  8. CString

    CString gray("Gray"); CString cat("Cat"); CString graycat = gray + cat;   与其用 sp ...

  9. 大熊君大话NodeJS之------Global Objects全局对象

    一,开篇分析 在上个章节中我们学习了NodeJS的基础理论知识,对于这些理论知识来说理解是至关重要的,在后续的章节中,我们会对照着官方文档逐步学习里面的各部分模块,好了该是本文主角登台亮相的时候了,G ...

  10. Jquery控制滚动显示欢迎字幕v2

    Jquery控制滚动显示欢迎字幕v2: 之前做的那个比较适合测试环境,但要套入到网站中,有两个按钮在那摆着,还是不太好看.后面对代码进行了修改,如下: 参考代码: <html> <h ...