hdu1054 树形dp&&二分图
Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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
#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&&二分图的更多相关文章
- HDU - 1054 Strategic Game(二分图最小点覆盖/树形dp)
d.一颗树,选最少的点覆盖所有边 s. 1.可以转成二分图的最小点覆盖来做.不过转换后要把匹配数除以2,这个待细看. 2.也可以用树形dp c.匈牙利算法(邻接表,用vector实现): /* 用ST ...
- HDU1054 Strategic Game —— 最小点覆盖 or 树形DP
题目链接:https://vjudge.net/problem/HDU-1054 Strategic Game Time Limit: 20000/10000 MS (Java/Others) ...
- HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP
分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...
- hdu1054 Strategic Game 树形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 思路:树形DP,用二分匹配也能解决 定义dp[root][1],表示以root 为根结点的子树且 ...
- hdu1520 Anniversary party 简单树形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 思路:树形DP的入门题 定义dp[root][1]表示以root为根节点的子树,且root本身参 ...
- 洛谷AT2046 Namori(思维,基环树,树形DP)
洛谷题目传送门 神仙思维题还是要写点东西才好. 树 每次操作把相邻且同色的点反色,直接这样思考会发现状态有很强的后效性,没办法考虑转移. 因为树是二分图,所以我们转化模型:在树的奇数层的所有点上都有一 ...
- 树形DP小结
树形DP1.简介:树是一种数据结构,因为树具有良好的子结构,而恰好DP是从最优子问题更新而来,那么在树上做DP操作就是从树的根节点开始深搜(也就是记忆化搜索),保存每一步的最优结果.tips:树的遍历 ...
- SPOJ 1479 +SPOJ 666 无向树最小点覆盖 ,第二题要方案数,树形dp
题意:求一颗无向树的最小点覆盖. 本来一看是最小点覆盖,直接一下敲了二分图求最小割,TLE. 树形DP,叫的这么玄乎,本来是线性DP是线上往前\后推,而树形DP就是在树上,由叶子结点状态向根状态推. ...
- Luogu P2458 [SDOI2006]保安站岗【树形Dp】
题目描述 五一来临,某地下超市为了便于疏通和指挥密集的人员和车辆,以免造成超市内的混乱和拥挤,准备临时从外单位调用部分保安来维持交通秩序. 已知整个地下超市的所有通道呈一棵树的形状:某些通道之间可以互 ...
随机推荐
- 股市非常态,CCI指标买卖点实例图解
CCI指标即顺势指标,是唐纳德·蓝伯特于上世纪80年代提出的,是一种比较新颖的技术指标.CCI指标是专门用来衡量股价是否超出常态分布范围,是一种 超买超卖类指标,但它与其他超买超卖型指标又有自己比较独 ...
- win10系统点击关机按钮后无法关机的解决办法
先吐槽下:我越发的发现我现在成了修电脑的了,我的职位是linux运维,现在干的活很蛋疼,公司只有我一个运维,修电脑.搞网络.抬服务器.弄监控,搭环境.搞自动化发布.弄虚拟化都我一个人哇.好了,打住. ...
- ecshop上传图片2
html代码 <tr> <td class="label">{$lang.lab_picture}</td> <td> <in ...
- GPRS/3G
像GPRS/3G模块之类的应用,需要连接,登陆,初始化等步骤完成后才能传输数据,而这些步骤又比较耗时. 所以用 状态机 + 超时 的机制来实现比较合理. 如下代码片段来描述数据透传 : 状态机 + 超 ...
- 年轻人,你活着不是为了观察K线做布朗运动
谈股票市场的赚钱陷阱 年轻人,你活着不是为了观察K线做布朗运动 作者:李晓鹏(2015-01-10) 这篇文章本来是该两年前写的,奉劝大家不要去玩股票.因为那个时候我的<中国崛起的经济学分析&g ...
- Python开发【第三篇】:Python基本数据类型
运算符 1.算数运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31- ...
- 由Collections.unmodifiableList引发的重构
原文 http://www.cnblogs.com/persist-confident/p/4516741.html 今天阅读源码的时候,无意中看到了Collections.unmodifiable ...
- iOS9 适配
iOS适配的相关内容的整理 之前iOS开发者一直很庆幸自己不用像安卓开发者那样适配各种不同类型的机型,但如今随着iPhone各种机型的改变,适配也成了我们开发中必须会的内容了.首先我们来了解一下对于不 ...
- [c#]RabbitMQ的简单使用
摘要 Message Queue消息队列,简称MQ,是一种应用程序对应用程序的通信方法,应用程序通过读写出入队列的消息来通信,而无需专用连接来链接它们.消息传递指的是程序之间通过在消息中发送数据进行通 ...
- HITtrainning20140417题解
题目列表: ID Origin Title 10 / 15 Problem A FZU 2152 文件系统 0 / 16 Problem B FZU 2153 A simple geome ...