树dp:边覆盖,点覆盖
#493. 求树的最小支配集
问题描述
对于一棵n个结点的无根树,求它的最小支配集。 最小支配集:指从所有顶点中取尽量少的点组成一个集合,使得剩下的所有点都与取出来的点有边相连。顶点个数最小的支配集被称为最小支配集。
输入格式
第一行一个整数n,表示结点数。接下来n-1行,每行两个整数a,b,表示结点a和b有边。
输出格式
一行,一个整数,表示最小支配集中元素的个数。
输入样例
9
2 5
2 7
2 8
9 2
3 2
3 1
3 6
4 3
输出样例
2
限制与预定
1 < n <=100000
时间限制:1s
空间限制:128mb
#include<cstdio>
using namespace std;
const int maxn=1e5+;
int n,f[maxn][];
//0->儿子,1->父亲,2->自己
inline int min(int a,int b){
return(a<b?a:b);
}
inline void read(int &ans){
ans=;int b=;
char x=getchar();
while(x<'' || ''<x){
if(x=='-')b=-;
x=getchar();
}
while(''<=x && x<=''){
ans=(ans<<)+(ans<<)+x-'';
x=getchar();
}
ans*=b;
}
int edge_count=,to[maxn*],next[maxn*],first[maxn];
inline void add_edge(int x,int y){
edge_count++;
to[edge_count]=y;
next[edge_count]=first[x];
first[x]=edge_count;
}
void search(int root,int fa)
{
int pos=;
for(int i=first[root];i;i=next[i]){
if(to[i]==fa)continue; search(to[i],root);
f[root][]+=f[ to[i] ][];
f[root][]+=min(f[ to[i] ][],f[ to[i] ][]);//+最小值 if( f[ to[i] ][]-f[ to[i] ][] < f[pos][]-f[pos][] )
pos=to[i];
}
f[root][]++;
if(f[root][]>f[root][])f[root][]=f[root][];
bool fd=;
for( int i=first[root];i;i=next[i] ){
if(to[i]==fa)continue;
fd=;
if(to[i]==pos){
f[root][]+=f[pos][];
}
else{
f[root][]+=min(f[to[i]][],f[to[i]][]);
}
}
f[root][]+=fd;
//if(root==9||root==8||root==7||root==5)printf("%d %d",root,f[root][0]);
//if(f[root][0]||f[root][1]||f[root][2] )printf("%d",root);
//if(root==1)printf("%d",f[1][0]);
}
int main()
{
//freopen("2.in","r",stdin); read(n);
for( int i=,a,b;i<n;i++){
read(a);read(b);
add_edge(a,b);
add_edge(b,a);
}
f[][]=0x7fffffff;
search(,);
printf("%d",min(f[][],min(f[][]+,f[][])));
return ;
}
树dp:边覆盖,点覆盖的更多相关文章
- CF456D A Lot of Games (字典树+DP)
D - A Lot of Games CF#260 Div2 D题 CF#260 Div1 B题 Codeforces Round #260 CF455B D. A Lot of Games time ...
- HDU4916 Count on the path(树dp??)
这道题的题意其实有点略晦涩,定义f(a,b)为 minimum of vertices not on the path between vertices a and b. 其实它加一个minimum ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- HDU4276 The Ghost Blows Light SPFA&&树dp
题目的介绍以及思路完全参考了下面的博客:http://blog.csdn.net/acm_cxlove/article/details/7964739 做这道题主要是为了加强自己对SPFA的代码的训练 ...
- Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)
[题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- bzoj 3572世界树 虚树+dp
题目大意: 给一棵树,每次给出一些关键点,对于树上每个点,被离它最近的关键点(距离相同被标号最小的)控制 求每个关键点控制多少个点 分析: 虚树+dp dp过程如下: 第一次dp,递归求出每个点子树中 ...
- bzoj 2286 [Sdoi2011]消耗战 虚树+dp
题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...
- (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland
Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- 51nod1812树的双直径(换根树DP)
传送门:http://www.51nod.com/Challenge/Problem.html#!#problemId=1812 题解:头一次写换根树DP. 求两条不相交的直径乘积最大,所以可以这样考 ...
随机推荐
- Platform.Uno介绍
编者语:Xamarin国内很多人说缺乏可用的实例,我在写书过程中在完善一些常用场景的例子,希望帮到大家.Build 2018结束一周了,善友问我要不要谈谈Xamarin的一些变化,但碍于时间有限一直没 ...
- Nginx ACCESS阶段 Satisfy 指令
L:60 这里一定要记住 return 指令所对应的阶段 早与access 因此如果location 有return 的话 那么 deny可能都会失效
- D. Maximum Diameter Graph 贪心+图论+模拟
题意:给出n个点的度数列 上限(实际点可以小于该度数列)问可以构造简单路最大长度是多少(n个点要连通 不能有平行边.重边) 思路:直接构造一条长链 先把度数为1的点 和度数大于1的点分开 先把度数 ...
- Vivado怎么使用In system debug(类似于chipscope的东西)
流程 1.建立好工程后,首先设置综合选项为保留设计的层次结构,方便追查信号.
- CodeForces 868F Yet Another Minimization Problem(决策单调性优化 + 分治)
题意 给定一个序列 \(\{a_1, a_2, \cdots, a_n\}\),要把它分成恰好 \(k\) 个连续子序列. 每个连续子序列的费用是其中相同元素的对数,求所有划分中的费用之和的最小值. ...
- Bigtable:A Distributed Storage System for Strctured Data
2006 年10 月Google 发布三架马车之一的<Bigtable:A Distributed Storage System for Strctured Data>论文之后,Power ...
- com.android.support:appcompat-v7:27.+问题解决
新建的项目就报下面的错误,问题指向了app/build.gradle,看起来是版本兼容的问题. 解决方案: 1.查看sdk tools的版本:26.1.1 远程依赖包是27.+,而我们的s ...
- elk中fliebeat的配置文件
fliebeat----> kafka的配置文件 # cat filebeat.yml|egrep -v "^$|^#"|grep -v "^ #" fi ...
- JProfiler性能分析工具
1.简介 JProfiler是一个商业授权的Java剖析工具,用于分析Java EE和Java SE应用程序. 2.JVMTI JDK本身定义了目标明确并功能完善的JNI(Java Native In ...
- SpringBoot系列: 使用 Swagger 生成 API 文档
SpringBoot非常适合开发 Restful API程序, 我们都知道为API文档非常重要, 但要维护好难度也很大, 原因有: 1. API文档如何能被方便地找到? 以文件的形式编写API文档都有 ...