树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. 求两条不相交的直径乘积最大,所以可以这样考 ...
随机推荐
- Activiti6事件及监听器配置(学习笔记)
1.事件及监听器原理 当流程引擎启动的时候,我们定义的监听器,就已经注册在一个事件类型上面. 注册的方式有多种,它可以注册在所有的事件类型上面.也可以注册在指定的几个事件类型上面,这样引擎启动的时候就 ...
- python json数据的转换
1 Python数据转json字符串 import json json_str = json.dumps(py_data) 参数解析: json_str = json.dumps(py_data,s ...
- Python中的string和bytes的转换
总的来说,bytes和string的关系是: \(bytes\xrightarrow{decode}string\) \(bytes\xleftarrow{encode}string\) 常见的几种编 ...
- x86汇编语言实践(1)
0 写在前面 为了更深入的了解程序的实现原理,近期我学习了IBM-PC相关原理,并手工编写了一些x86汇编程序. 在2017年的计算机组成原理中,曾对MIPS体系结构及其汇编语言有过一定的了解,考虑到 ...
- java 11 ZGC(可伸缩,低延迟的gc)
ZGC, A Scalable Low-Latency Garbage Collector(Experimental) 可伸缩,低延迟的gc ZGC, 这应该是JDK11最为瞩目的特性, 没有之一. ...
- vm Linux centos 链接外网
修改network配置 vi /etc/sysconfig/network-scripts/ifcfg-ens33 修改ONBOOT=yes 重启服务 service network restart ...
- Ubuntu16.04 安装g++6
https://blog.csdn.net/qq_34877350/article/details/81182022 1.安装gcc-6: sudo apt-get update && ...
- 20175209 《Java程序设计》第二周学习总结
教材学习内容总结 二三章介绍的主要是Java中的基本知识:数据类型及转换,数据的输入输出,数组,运算符表达式,和常见的一些语句,这些都是帮助我们学习Java的基本知识,而这些知识很大一部分都和C语言相 ...
- grovvy身份证(全)
import java.text.DecimalFormat import java.text.SimpleDateFormat import java.util.Calendar; import j ...
- java matlab 混合编程 Failed to find the required library mclmcrrt9_2.dll on java.library.path.
问题描述: Exception in thread "main" java.lang.UnsatisfiedLinkError: Failed to find the requir ...