CF633F The Chocolate Spree

LVI.CF633F The Chocolate Spree

奇奇怪怪的直径题

思路1.用多种东西拼出来直径

我们设f[i][0/1/2/3]表示:

0:子树内一条路径的最大值

1:子树内两条路径的最大值

2:子树内一条路径,且起点为x的最大值

3:子树内两条路径,且有一条起点为x的最大值

则答案为f[1][1]

考虑如何转移。

sonxx的儿子集合。

则:

f[x][0]=max{maxysonxf[y][0],f[p][2]+f[q][2]+valx}

f[x][1]

可以是子树f[1]的最大值;

可以通过子树里面一个f[0],再加上(f[2]+f[2]+valx)拼出的一条路径构成;

也可以通过f[3]+f[2]+valx构成。

f[x][2]=maxysonxf[y][2]+valx

f[x][3]=f[p][2]+f[q][1]+valx

至于pq的选择,可以只记录f[0]f[1]f[3]前三大的值,也可以直接偷懒vector排序水过。

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long 
int n,val[100100],head[100100],f[100100][4],cnt,res;
//0:a chain in the subtree
//1:two chains in the subtree
//2:a chain in the subtree with x is the starting point
//3:two chains in the subtree with x is one of the staring points
struct node{
	int to,next;
}edge[200100];
void ae(int u,int v){
	edge[cnt].next=head[u],edge[cnt].to=v,head[u]=cnt++;
	edge[cnt].next=head[v],edge[cnt].to=u,head[v]=cnt++;
}
void match(int x,int a,int b,int c){//use half chains from A and B to form a complete chain, and use a full chain from C.
	if(a!=b&&b!=c&&c!=a)f[x][1]=max(f[x][1],f[a][2]+f[b][2]+val[x]+f[c][0]);
}
void dfs(int x,int fa){
	vector<pair<int,int> >v0,v2,v3;
	for(int i=head[x],y;i!=-1;i=edge[i].next){
		if((y=edge[i].to)==fa)continue;
		dfs(edge[i].to,x);
		f[x][0]=max(f[x][0],f[edge[i].to][0]);
		f[x][1]=max(f[x][1],f[edge[i].to][1]);
		f[x][2]=max(f[x][2],f[edge[i].to][2]);
		f[x][3]=max(f[x][3],f[edge[i].to][3]);
		v0.push_back(make_pair(f[edge[i].to][0],edge[i].to));
		v2.push_back(make_pair(f[edge[i].to][2],edge[i].to));
		v3.push_back(make_pair(f[edge[i].to][3],edge[i].to));
	}
	f[x][2]+=val[x],f[x][3]+=val[x];
	sort(v0.begin(),v0.end()),reverse(v0.begin(),v0.end());
	while(v0.size()<3)v0.push_back(make_pair(0,0));
	sort(v2.begin(),v2.end()),reverse(v2.begin(),v2.end());
	while(v2.size()<3)v2.push_back(make_pair(0,0));
	sort(v3.begin(),v3.end()),reverse(v3.begin(),v3.end());
	while(v3.size()<3)v3.push_back(make_pair(0,0));
	f[x][0]=max(f[x][0],v2[0].first+v2[1].first+val[x]);
	f[x][1]=max(f[x][1],v0[0].first+v0[1].first);
	if(v0[0].second!=v2[0].second)f[x][3]=max(f[x][3],v0[0].first+v2[0].first+val[x]);
	else f[x][3]=max(f[x][3],max(v0[0].first+v2[1].first,v0[1].first+v2[0].first)+val[x]);
	for(int i=0;i<3;i++)for(int j=0;j<3;j++)for(int k=0;k<3;k++)match(x,v2[i].second,v2[j].second,v0[k].second);
	if(v2[0].second!=v3[0].second)f[x][1]=max(f[x][1],v2[0].first+v3[0].first+val[x]);
	else f[x][1]=max(f[x][1],max(v2[1].first+v3[0].first,v2[0].first+v3[1].first)+val[x]);
}
signed main(){
	scanf("%lld",&n),memset(head,-1,sizeof(head));
	for(int i=1;i<=n;i++)scanf("%lld",&val[i]);
	for(int i=1,x,y;i<n;i++)scanf("%lld%lld",&x,&y),ae(x,y);
	dfs(1,0);
	printf("%lld\n",f[1][1]);
	return 0;
}

思路2.二次扫描+换根

因为这两条路径一定会被一条边分成两半,两条路径各在一半里面,所以可以换根换出最大的断边。

f[x]表示x的子树中,以x为起点的路径的最大值

g[x]表示x子树的直径。

h[x]表示除了x子树外的其余部分,以x的父亲为起点的路径最大值

d[x]表示除了x子树外剩余部分的直径。

则答案为max(g[x]+d[x])

fg可以一遍普通DP就能算出来;

我们设v集合表示在x的儿子中从大到小排序后的f集合,

u集合表示在x的儿子中从大到小排序后的g集合,

d[x]可以从父亲边选一条,儿子边选一条;或者选两条儿子边;或者继承父亲的d或儿子的g

即:

d[y]=max({d[x],max(h[x],(y==v[0]||y==v[1]?f[v[2]]:f[v[1]]))+(y==v[0]?f[v[1]]:f[v[0]])+val[x],(y==u[0]?g[u[1]]:g[u[0]])});

h[x]可以选择继承父亲的,也可以选择另一个兄弟的f,即:

h[y]=max(h[x],(y==v[0]?f[v[1]]:f[v[0]]))+val[x];

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,f[100100],g[100100],h[100100],d[100100],head[100100],cnt,res,val[100100];
struct node{
	int to,next;
}edge[200100];
void ae(int u,int v){
	edge[cnt].next=head[u],edge[cnt].to=v,head[u]=cnt++;
}
void dfs1(int x,int fa){
	g[x]=val[x];
	for(int i=head[x],y;i!=-1;i=edge[i].next){
		if((y=edge[i].to)==fa)continue;
		dfs1(y,x);
		g[x]=max(g[x],f[x]+f[y]+val[x]);
		g[x]=max(g[x],g[y]);
		f[x]=max(f[x],f[y]);
	}
	f[x]+=val[x];
}
bool cmp1(const int &x,const int &y){
	return f[x]>f[y];
}
bool cmp2(const int &x,const int &y){
	return g[x]>g[y];
}
void dfs2(int x,int fa){
	vector<int>v,u;
	for(int i=head[x],y;i!=-1;i=edge[i].next){
		if((y=edge[i].to)==fa)continue;
		v.push_back(edge[i].to),u.push_back(edge[i].to);
	}
	sort(v.begin(),v.end(),cmp1),v.push_back(0),v.push_back(0);
	sort(u.begin(),u.end(),cmp2),u.push_back(0);
	for(int i=head[x],y;i!=-1;i=edge[i].next){
		if((y=edge[i].to)==fa)continue;
		d[y]=max({d[x],max(h[x],(y==v[0]||y==v[1]?f[v[2]]:f[v[1]]))+(y==v[0]?f[v[1]]:f[v[0]])+val[x],(y==u[0]?g[u[1]]:g[u[0]])});
		res=max(res,g[y]+d[y]);
//		printf("(%d,%d):%d,%d,%d,%d\n",x,y,g[y],max(h[x],(y==v[0]||y==v[1]?f[v[2]]:f[v[1]])),(y==v[0]?f[v[1]]:f[v[0]]),val[x]);
		h[y]=max(h[x],(y==v[0]?f[v[1]]:f[v[0]]))+val[x];
		dfs2(y,x);
	}
}
signed main(){
	scanf("%lld",&n),memset(head,-1,sizeof(head));
	for(int i=1;i<=n;i++)scanf("%lld",&val[i]);
	for(int i=1,x,y;i<n;i++)scanf("%lld%lld",&x,&y),ae(x,y),ae(y,x);
	dfs1(1,0),dfs2(1,0);
//	for(int i=1;i<=n;i++)printf("%lld ",f[i]);puts("");
//	for(int i=1;i<=n;i++)printf("%lld ",g[i]);puts("");
//	for(int i=1;i<=n;i++)printf("%lld ",h[i]);puts("");
	printf("%lld\n",res);
	return 0;
}

posted @   Troverld  阅读(76)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示