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】
题目描述 五一来临,某地下超市为了便于疏通和指挥密集的人员和车辆,以免造成超市内的混乱和拥挤,准备临时从外单位调用部分保安来维持交通秩序. 已知整个地下超市的所有通道呈一棵树的形状:某些通道之间可以互 ...
随机推荐
- 深入浅出MyBatis
参考文献:深入浅出MyBatis MyBatis功能架构图: MyBatis内部原理流程图: 详情见:深入浅出MyBatis
- 安装weinre在PC端调试移动端
1.使用node安装weinre. 2.启动weinre, weinre --httpPort 8081 --boundHost -all- 3.在浏览器打开 http://localhost:80 ...
- 常用的JS HTML DOM 事件
HTML DOM 事件 HTML DOM 事件允许Javascript在HTML文档元素中注册不同事件处理程序. 事件通常与函数结合使用,函数不会在事件发生前被执行! (如用户点击按钮). 提示: 在 ...
- 无忧之道:Docker中容器的备份、恢复和迁移
原创:LCTT https://linux.cn/article-5967-1.html译者: GOLinux本文地址:https://linux.cn/article-5967-1.html 201 ...
- ProFTPD <=1.3.5 mod_copy 未授权文件复制漏洞
poc如下: #!/usr/bin/env python# coding=utf-8 """Site: http://www.beebeeto.com/Framework ...
- Entity Framework - Using Transactions or SaveChanges(false) and AcceptAllChanges()?
LINK With the Entity Framework most of the time SaveChanges() is sufficient. This creates a transact ...
- java中如何将字符串数组转换成字符串(转)
如果是 “字符串数组” 转 “字符串”,只能通过循环,没有其它方法 String[] str = {"abc", "bcd", "def"} ...
- WKWebView使用及注意点
iOS8之后,苹果推出了WebKit这个框架,用来替换原有的UIWebView,新的控件优点多多,不一一叙述.由于一直在适配iOS7,就没有去替换,现在仍掉了iOS7,以为很简单的就替换过来了,然而在 ...
- Highcharts属性中英文参照
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- VPN添加静态路由表(指定程序或资源走VPN)
在某此情况下,我们希望为VPN客户端指定线路,比如只有公司的资源或网站才使用VPN连接,其它的网络请求依然走他们自己的默认网关. 这种情况下,我们就需要给VPN客户端添加静态路由规则并取消VPN连接的 ...