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】
题目描述 五一来临,某地下超市为了便于疏通和指挥密集的人员和车辆,以免造成超市内的混乱和拥挤,准备临时从外单位调用部分保安来维持交通秩序. 已知整个地下超市的所有通道呈一棵树的形状:某些通道之间可以互 ...
随机推荐
- HTML学习笔记——head、body及简单标签
1> title标签.网站关键词.网站描述.实现百度网的跳转 2> 单标签.对标签.p标签 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...
- Eclipse查看hadoop源代码出现Source not found,是因为没有添加.zip
在我们hadoop编程中,经常遇到像看看hadoop的某个类中函数的功能.但是我们会遇到一种情况就是Source not found.遇到这个问题,该如何解决.因为我们已经引入了包,为什么会找不到.如 ...
- conv2、filter2、imfilter的区别
conv2.filter2.imfilter的区别 -------------------------------------conv2函数------------------------------ ...
- php生成excle
方法一: 新建index.php,代码如下 <?php header("Content-type:application/vnd.ms-excel"); header(&qu ...
- UIAlertController 标题文字大小 颜色
NSString *title = [NSString stringWithFormat:]; NSString *msg = @"\n把红包分享给微信好友,金额随机,可用于购买雪票和雪卡& ...
- JavaScript 函数参数传递到底是值传递还是引用传递
tips:这篇文章是听了四脚猫的js课程后查的,深入的理解可以参看两篇博客: JavaScript数据类型--值类型和引用类型 JavaScript数据操作--原始值和引用值的操作本质 在传统的观念里 ...
- [转]Sql按年份.月份.每天统计数量
1.每年 select year(ordertime) 年, sum(Total) 合计 from 表 group by year(ordertime) 2.每月 select year(ordert ...
- wamp包--如何导出sql
在windows下安装wamp,如果不想用phpmyadmin工具和其他工具,如何导出自己想要的sql呢. 比如:我想导出blogyaf库,可以从以下步骤进行操作. 1,进入到wamp的mysql安装 ...
- CF467D Fedor and Essay 建图DFS
Codeforces Round #267 (Div. 2) CF#267D D - Fedor and Essay D. Fedor and Essay time limit per test ...
- Java当中的内存分配以及值传递问题内存解析
首先必须说明作为Java程序员对于内存只要有大致的了解就可以了,如果你对Java当中的某一个知识点在不需要分析内存分配过程的情况下可以掌握,那就大可不必去研究内存.如果你对知识点已经掌握,那么你应该把 ...