树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. 求两条不相交的直径乘积最大,所以可以这样考 ...
随机推荐
- python之路4-文件操作
对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 f = open('lyrics','r',encoding='utf-8') read_line = f.r ...
- python之旅十【第十篇】paramiko模块
paramiko模块介绍 ssh的远程连接 基于用户名密码的连接 import paramiko # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_h ...
- BZOJ1975[Sdoi2010]魔法猪学院——可持久化可并堆+最短路树
题目描述 iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPig对猪世界的世界本原有了很多的了解:众所周知,世界是由元素构成的:元素与 ...
- UOJ#310.【UNR #2】黎明前的巧克力(FWT)
题意 给出 \(n\) 个数 \(\{a_1, \cdots, a_n\}\),从中选出两个互不相交的集合(不能都为空),使得第一个集合与第二个集合内的数的异或和相等,求总方案数 \(\bmod 99 ...
- MySQL安装-二进制软件包安装
MySQL 双版本安装 安装mysql AB (RPM) -mysql官方的RPM包 安装MySQL 5.6.19版本 安装之前需要将系统自带的关于mysql软件全部卸载掉 rpm -e (加 ...
- python xpath学习
一.选取节点: 二.谓词: 注意:在scrapy中用xpath进行搜索时,如果使用相对路径,要加上.,如,不然搜索的是整个文档.
- 测试框架httpclent 3.获取cookie的信息,然后带cookies去发送请求
在properties文件里面: startupWithCookies.json [ { "description":"这是一个会返回cookies信息的get请求&qu ...
- Docker下安装Jenkins
Docker安装参见:https://www.cnblogs.com/hackyo/p/9280042.html 安装Jenkins: docker run \ -u root \ --rm \ -d ...
- css选择器(常规选择器,伪类选择器,伪元素选择器,根元素选择器)
前言 CSS的一个核心特性是能向文档中的一组元素类型应用某些规则,本文将详细介绍CSS选择器 选择器 [通配选择器] 星号*代表通配选择器,可以与任何元素匹配 *{color: red;} [元素选择 ...
- 【转载】VS中生成、清理项目、调试、开始执行(不调试)、Debug 和 Release等之间的区别
https://blog.csdn.net/u012441545/article/details/51404412