题目意思: 一棵树,找到最少的点能覆盖到所有的边,(也就是每条边俩端 至少有一个在你找到的集合);

  解法:每条边只能被俩个点中的一个,或全部覆盖所以我们有树形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的更多相关文章

  1. SPOJ 1435 Vertex Cover 树形DP

    i 表示节点 i ,j=0表示不选择其父节点,j=1表示选择其父节点.f 为其父节点. 取 每个节点选择/不选择 两者中较小的那个. 一组数据: 151 21 31 41 1010 910 1112 ...

  2. SPOJ 1435 - Vertex Cover(树形DP,树的最小点覆盖)

    算是个经典题目了,很模板的树形DP题目 做这个题的时候一开始就想到树形DP了,可是由于各种原因没写出来,代码太糟烂了,赛后还是改了好久才过的 dp(u,0)=sum(dp(v,1)): dp(u,1) ...

  3. 【题解】Luogu SP1435 PT07X - Vertex Cover

    原题传送门 求树的最小点覆盖,就是一个树形dp 类似于没有上司的舞会 dp的状态为\(f[i][0/1]\),表示i节点是否选择 边界是\(f[x][0]=0\),\(f[x][1]=1\) 转移方程 ...

  4. 集合覆盖 顶点覆盖: set cover和vertex cover

    这里将讲解一下npc问题中set cover和vertex cover分别是什么. set cover: 问题定义: 实例:现在有一个集合A,其中包含了m个元素(注意,集合是无序的,并且包含的元素也是 ...

  5. 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 ...

  6. PAT1134:Vertex Cover

    1134. Vertex Cover (25) 时间限制 600 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A vertex ...

  7. 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 ...

  8. 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 ...

  9. PAT 甲级 1134 Vertex Cover

    https://pintia.cn/problem-sets/994805342720868352/problems/994805346428633088 A vertex cover of a gr ...

随机推荐

  1. HTML5系列之——applicationCache对象

    ApplicationCache主要简单介绍: applicationCache对象实现HTML5相应WEB离线功能.以下我们来主要解说applicationCache对象的一些主要功能和方法 app ...

  2. POJ 3340 &amp; HDU 2410 Barbara Bennett&#39;s Wild Numbers(数学)

    题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Descript ...

  3. sql使用存储过程和交易

    在过去的一年.学习数据库的时候学校有存储过程.永远只是知道一些理论,我不知道怎么用.时隔一年,最终找到怎样使用存储过程了. 在机房收费系统中.有些操作.须要多次运行sql语句,多次运行完毕才算是完毕这 ...

  4. cocos2d-x CCNode类

    文章引用自http://blog.csdn.net/qiurisuixiang/article/details/8763260 1 CCNode是cocos2d-x中一个非常重要的类.CCNode是场 ...

  5. jquery ui中 accordion的问题及我的解决方法

    原文:jquery ui中 accordion的问题及我的解决方法 jquery有一套所谓的ui组件,很不错的.如果有兴趣的朋友,可以参考http://jqueryui.com/ 但其中的accord ...

  6. mysql联合索引的应用

    有一个log表,结构是这样的: CREATE TABLE `weblog` (   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,   `ip` varc ...

  7. Unity MVC框架 StrangeIoC

    StrangeIoC是一个超轻量级和高度可扩展的控制反转(IoC)框架,专门为C#和Unity编写. 项目地址:https://github.com/strangeioc/strangeioc 文档地 ...

  8. CentOS修改yum更新源

    1. 在修改前先备份该文件 cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak 2. 修改更新源配置文 ...

  9. POJ 2411

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9614   Accepted: 5548 ...

  10. MyEclipse中“擅自乱改”项目名导致项目报错的处理

    最近几天培训的过程中,经常有同学手一抖,默默的修改了本来配置部署好的项目名,导致项目报错…… 遇到这种事情,我一般会做的处理就是重新新建项目,然后把包和各种文件ctrl+c ctrl+v,遇到项目小还 ...