SPOJ PT07X Vertex Cover
题目意思: 一棵树,找到最少的点能覆盖到所有的边,(也就是每条边俩端 至少有一个在你找到的集合);
解法:每条边只能被俩个点中的一个,或全部覆盖所以我们有树形DP来解:
DP[num][flag]//代表在子树NUM全部被覆盖的情况下,flag=1,这个店也被覆盖:flag=false 这个店没被覆盖;
那么有了这样的想法大妈就很好写了毕竟树形DP 主要的初始化和合并的状态:
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
const int maxn=;
int n;
int dp[maxn][];
struct Edge
{
int to,pre;
Edge (int to=,int pre=):to(to),pre(pre){}
};
Edge edge[maxn*];
int head[maxn],pos;
inline void add_edge(int s,int to)
{
edge[pos]=Edge(to,head[s]);
head[s]=pos++;
}
inline void inint()
{
memset(head,-,sizeof(head));
pos=;
}
void dfs(int pa,int s)
{
dp[s][]=;
dp[s][]=;
for(int w=head[s];~w;w=edge[w].pre)
{
Edge &tmp=edge[w];
if(tmp.to==pa)continue;
dfs(s,tmp.to);
dp[s][]+=dp[tmp.to][];
dp[s][]+=min(dp[tmp.to][],dp[tmp.to][]);
}
}
int main()
{
int a,b;
while(~scanf("%d",&n))
{
inint();
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
scanf("%d%d",&a,&b);
add_edge(a,b);
add_edge(b,a);
}
dfs(-,);
printf("%d\n",min(dp[][],dp[][]));
}
return ;
}
SPOJ PT07X Vertex Cover的更多相关文章
- SPOJ 1435 Vertex Cover 树形DP
i 表示节点 i ,j=0表示不选择其父节点,j=1表示选择其父节点.f 为其父节点. 取 每个节点选择/不选择 两者中较小的那个. 一组数据: 151 21 31 41 1010 910 1112 ...
- SPOJ 1435 - Vertex Cover(树形DP,树的最小点覆盖)
算是个经典题目了,很模板的树形DP题目 做这个题的时候一开始就想到树形DP了,可是由于各种原因没写出来,代码太糟烂了,赛后还是改了好久才过的 dp(u,0)=sum(dp(v,1)): dp(u,1) ...
- 【题解】Luogu SP1435 PT07X - Vertex Cover
原题传送门 求树的最小点覆盖,就是一个树形dp 类似于没有上司的舞会 dp的状态为\(f[i][0/1]\),表示i节点是否选择 边界是\(f[x][0]=0\),\(f[x][1]=1\) 转移方程 ...
- 集合覆盖 顶点覆盖: set cover和vertex cover
这里将讲解一下npc问题中set cover和vertex cover分别是什么. set cover: 问题定义: 实例:现在有一个集合A,其中包含了m个元素(注意,集合是无序的,并且包含的元素也是 ...
- URAL 2038 Minimum Vertex Cover
2038. Minimum Vertex Cover Time limit: 1.0 secondMemory limit: 64 MB A vertex cover of a graph is a ...
- PAT1134:Vertex Cover
1134. Vertex Cover (25) 时间限制 600 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A vertex ...
- A1134. Vertex Cover
A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...
- PAT A1134 Vertex Cover (25 分)——图遍历
A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...
- PAT 甲级 1134 Vertex Cover
https://pintia.cn/problem-sets/994805342720868352/problems/994805346428633088 A vertex cover of a gr ...
随机推荐
- 如何搭建DNS服务(转)
继NTP时间服务器后,继续搭建DNS服务,鉴于昨晚撰写时间超过预期,这次改变策略,先把自己需要用到的部分写出来(主要是基于RAC的搭建,只涉及正向和反向DNS解析),后面再添加必要的说明和阐述. 试验 ...
- VMware中linux与window目录共享
在虚拟机下来实如今windows下共享一个目录: (前提已安装完毕vmtools:http://blog.csdn.net/pipisorry/article/details/21318931) 打开 ...
- form表单中的 action=./?> 是什么意思
./代表当前目录,?代表查询字符串为空 action="" //一般可以为空的,这里的双引号都要有的,表示提单提交给自己(也就是当前页处理)action="a.php&q ...
- ADN中国团队參加微软的Kinect全国大赛获得三等奖
上周末我们团队參加了微软的Kinect全国大赛,我们的Kinect + Navisworks漫游荣膺三等奖 团队经理Joe写了篇详实的总结,我就直接转载了. http://blog.csdn.ne ...
- 日期格式化标签<fmt:formatDate>&<fmt:setTimeZone>时区标签的使用demo
日期格式化标签<fmt:formatDate>&<fmt:setTimeZone>时区标签的使用demo <%@ page contentType="t ...
- C#多线程问题整合
一.跨进程访问组件 错误:线程间操作无效: 从不是创建控件“XXX”的线程访问它 解决方法: 1:把CheckForIllegalCrossThreadCalls设置为false 这个方法只是不去捕获 ...
- python学习1(小白记录)
python创建cocos2d-x项目注意点1. 2.7.5版本号的.配置好环境变量之后.要切换到tools文件夹下.直接运行 python create_project.py ..........这 ...
- SVN中正确的add操作和delete操作
add操作: delete操作:
- Webserver管理系列:1、安装Windows Server 2008
简单了解下server: 1U: 2U: 3U: 在安装Windows Server 2008之前我们先了解下Windows Server 2008: Windows Server 2008是微软一个 ...
- 【leetcode】Candy(python)
题目要求的比它的邻居比自己奖励,因此,我们有最少一个多的.所有我们可以找到所有的坑,凹坑例如,存在以下三种情况. 找到全部的凹点后,我们就能够从凹点处開始向左右两个方向依次查找递增序列.当中每一个高的 ...