#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:边覆盖,点覆盖的更多相关文章

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

  2. HDU4916 Count on the path(树dp??)

    这道题的题意其实有点略晦涩,定义f(a,b)为 minimum of vertices not on the path between vertices a and b. 其实它加一个minimum ...

  3. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  4. HDU4276 The Ghost Blows Light SPFA&&树dp

    题目的介绍以及思路完全参考了下面的博客:http://blog.csdn.net/acm_cxlove/article/details/7964739 做这道题主要是为了加强自己对SPFA的代码的训练 ...

  5. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  6. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  7. bzoj 3572世界树 虚树+dp

    题目大意: 给一棵树,每次给出一些关键点,对于树上每个点,被离它最近的关键点(距离相同被标号最小的)控制 求每个关键点控制多少个点 分析: 虚树+dp dp过程如下: 第一次dp,递归求出每个点子树中 ...

  8. bzoj 2286 [Sdoi2011]消耗战 虚树+dp

    题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...

  9. (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland

    Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...

  10. 51nod1812树的双直径(换根树DP)

    传送门:http://www.51nod.com/Challenge/Problem.html#!#problemId=1812 题解:头一次写换根树DP. 求两条不相交的直径乘积最大,所以可以这样考 ...

随机推荐

  1. fetch和axios获取数据

    fetch("/api/goods") .then(res => { return res.json(); }) .then(response => { console ...

  2. Java 异常体系

    1.异常简介 Java把异常作为一种类,当做对象来处理.所有异常类的基类是Throwable类,两大子类分别是Error和Exception. 系统错误由Java虚拟机抛出,用Error类表示.Err ...

  3. BZOJ 2243 染色

    树链剖分+区间染色 因为是一颗树不是森林,所以应该用树剖就行,但是LCT好像也能写.. 直接用线段树维护树上的节点,注意pushdown还有询问的时候要考虑区间相交的地方,也就是左孩子右边和有孩子的左 ...

  4. CSS当中数学表达式calc

    CSS当中数学表达式calc  数学表达式calc()是CSS中的函数,主要用于数学运算.使用calc()为页面元素布局提供了便利和新的思路.本文将介绍calc()的相关内容 定义 数学表达式calc ...

  5. java java.net.URLConnection 实现http get,post

    抽象类 URLConnection 是所有类的超类,它代表应用程序和 URL 之间的通信链接.此类的实例可用于读取和写入此 URL 引用的资源.通常,创建一个到 URL 的连接需要几个步骤: open ...

  6. Swagger - ui 学习

    今天同组的打伙伴给介绍了 Swagger-ui 这个 自动生成 接口文档的 工具,感觉比较方便好用, 遂决定 学习一下, 开个随笔进行随时记录,同时也是提醒自己 先保存两篇感觉还不错的文章 : htt ...

  7. Eclipse拷贝动态的web工程修改context root的值

    Eclipse拷贝动态的web工程修改context root的值 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. context root的名称一般是我们访问URL时的PATH路径 ...

  8. crm 数据展示 和分页思想(一)

    1. 数据的展示 数据通过ORM查询出来 对象列表 QuerySet 1. 普通的字段 对象.字段名 ——> 数据库中的值 <td>{{ customer.phone }}</ ...

  9. postgreSql 常用操作总结

    0. 启动pgsl数据库 pg_ctl -D /xx/pgdata start 1. 查看pgsl版本 pg_ctl --version 1. 命令行登录数据库 psql -U username -d ...

  10. gdb core 调试多线程

    ref :http://blog.sina.com.cn/s/blog_62dc94eb0100flyn.html 如果目标进程已经core dump了,那么 gdb -c core xxx   xx ...