BZOJ 1907 树的路径覆盖 树形DP

题目大意:给定一棵树,求最小路径覆盖

数据范围1W,看到还想跑网络流来着= = 不过算了明明树形DP这么水还是不要用网络流这种大杀器为好

首先将所有的链都考虑成以链上所有点的LCA为转折点的V字形

那么点有两种:转折点和非转折点

因此我们选择两种状态进行转移:还会和父亲组成链的状态和成为转折点的状态

转移就自己YY算了

时间复杂度是线性的

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 10100
using namespace std;
struct abcd{
	int to,next;
}table[M<<1];
int head[M],tot;
int n,f[M][2];
//0-ここでは终わり
//1-まで続いている
void Add(int x,int y)
{
	table[++tot].to=y;
	table[tot].next=head[x];
	head[x]=tot;
}
void Initialize()
{
	memset(head,0,sizeof head);tot=0;
}
void Tree_DP(int x,int from)
{
	int i,temp=0;
	f[x][0]=f[x][1]=1;
	for(i=head[x];i;i=table[i].next)
		if(table[i].to!=from)
		{
			Tree_DP(table[i].to,x);

			f[x][0]=min(f[x][0]+f[table[i].to][0],f[x][1]+f[table[i].to][1]-1);
			f[x][1]=min(f[x][1]+f[table[i].to][0],temp+f[table[i].to][1]);
			temp+=f[table[i].to][0];
		}
}
int main()
{
	int T,i,x,y;
	for(cin>>T;T;T--)
	{
		Initialize();
		cin>>n;
		for(i=1;i<n;i++)
		{
			scanf("%d%d",&x,&y);
			Add(x,y);Add(y,x);
		}
		Tree_DP(1,0);
		cout<<f[1][0]<<endl;
	}
	return 0;
}
时间: 2024-07-30 13:35:29

BZOJ 1907 树的路径覆盖 树形DP的相关文章

bzoj 1907: 树的路径覆盖【贪心+树形dp】

我是在在做网络流最小路径覆盖的时候找到这道题的 然后发现是个贪心+树形dp \( f[i] \)表示在\( i \)为根的子树中最少有几条链,\( v[i] \) 表示在\( i \)为根的子树中\( i \) 是( 0)否(1)为一条链的端点 然后贪心转移即可(有链端点则连起来) #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=10005; i

【bzoj1907】树的路径覆盖 树形dp

题目描述 输入 输出 样例输入 1 7 1 2 2 3 2 4 4 6 5 6 6 7 样例输出 3 题解 树形dp 设f[x]表示以x为根的子树完成路径覆盖,且x为某条路径的一端(可以向上延伸)的最小路径数,g[x]表示以x为根的子树完成路径覆盖,且x不为某条路径的一端的最小路径数. 那么考虑点x,只有三种情况:单独成路径.与一条子树的链成路径.与两条子树的链成路径. 这三种情况分别对应三种状态转移方程,具体见代码. 然而看到网上题解大把大把的贪心我也是醉了qaq #include <cstd

bzoj 1907 树的路径覆盖 贪心

题面 题目传送门 解法 给个链接,贪心和树形dp讲得挺到位的 题解 反正我觉得贪心比较好写,也挺好理解的 时间复杂度:\(O(Tn)\) 代码 #include <bits/stdc++.h> #define N 10010 using namespace std; template <typename node> void chkmax(node &x, node y) {x = max(x, y);} template <typename node> voi

1907: 树的路径覆盖

1907: 树的路径覆盖 Time Limit: 5 Sec  Memory Limit: 259 MBSubmit: 506  Solved: 219[Submit][Status][Discuss] Description Input Output Sample Input 1 7 1 2 2 3 2 4 4 6 5 6 6 7 Sample Output 3 HINT Source Play with Tree By Amber 题解:贪心题么么哒,直接DFS一遍就好啦 1 /******

BZOJ1907 树的路径覆盖

ydc题解上写着贪心,后来又说是树形dp...可惜看不懂(顺便骗三连) 其实就是每个叶子开始拉一条链,从下面一路走上来,遇到能把两条链合起来的就合起来就好了. 1 /************************************************************** 2 Problem: 1907 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:112 ms 7 Memory:1396 kb 8 *****

bzoj 1017[JSOI2008]魔兽地图DotR - 树形dp

1017: [JSOI2008]魔兽地图DotR Time Limit: 30 Sec  Memory Limit: 162 MB Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA (Defense of the Ancients) Allstars.DotR里面的英雄只有一个属性--力量.他们需要购买装备来提升自己的力量值,每件装备都可以使佩戴它的英雄的力量值提高固定的点数,所以英雄

BZOJ 3238: [Ahoi2013]差异 后缀自动机 树形dp

http://www.lydsy.com/JudgeOnline/problem.php?id=3238 就算是全局变量,也不要忘记,初始化(吐血). 长得一副lca样,没想到是个树形dp(小丫头还有两幅面孔呢). 看代码实现吧,不大容易口头解释,把加的和减的分开算就可以了,减去的通过倒着建sam(相当于建一棵后缀树),然后算每个len取的次数实现,注意树归中一些避免重复操作. 1 /********************************************************

树的直径,树形DP,DFS——POJ1958

题目链接 题目含义 就是建一个树,让你求最大直径 下面用分别用DP和DFS的方法 题目代码 #include<iostream> #include<stdio.h> #include<string.h> using namespace std; const int maxn=5e4+7; int n,m,tot,a,b,c,ans; char cc; int head[maxn],d[maxn]; struct node{ int to,w,next; }edge[ma

BZOJ 1509: [NOI2003]逃学的小孩( 树形dp )

树形dp求出某个点的最长3条链a,b,c(a>=b>=c), 然后以这个点为交点的最优解一定是a+2b+c.好像还有一种做法是求出树的直径然后乱搞... --------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<cctype> using