Codeforces - F. Up and Down the Tree

题目链接:Codeforces - F. Up and Down the Tree


考虑树DP。

我们可以先考虑走了之后还能回来的最大价值。。

然后我们再考虑有一次走到底,不用回来的来更新最大值。

AC代码:

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=1e6+10;
int n,k,dp[N],low[N],res[N],dep[N];
vector<int> g[N];
void dfs1(int x,int fa){
	dep[x]=dep[fa]+1;
	if(g[x].size()==0){
		dp[x]=1;	low[x]=dep[x];	return ;
	}
	int mi=1e9,val=0;
	for(auto to:g[x]){
		dfs1(to,x);
		if(low[to]-dep[x]<=k){
			val+=dp[to];	mi=min(mi,low[to]);
		}
	}
	low[x]=mi;	dp[x]=val;
}
void dfs2(int x){
	res[x]=dp[x];
	for(auto to:g[x]){
		dfs2(to);	int tmp=dp[x];
		if(low[to]-dep[x]<=k)	tmp-=dp[to];
		res[x]=max(res[x],tmp+res[to]);
	}
}
signed main(){
	ios::sync_with_stdio(false);	cin.tie(nullptr);
	cin>>n>>k;
	for(int i=2,x;i<=n;i++)	cin>>x,g[x].push_back(i);
	dfs1(1,0);	dfs2(1);
	cout<<res[1];
	return 0;
}
发布了475 篇原创文章 · 获赞 241 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43826249/article/details/104091818