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]\)

考虑如何转移。

\(son_x\)\(x\)的儿子集合。

则:

\(f[x][0]=\max\{\max\limits_{y\in son_x}f[y][0],f[p][2]+f[q][2]+val_x\}\)

\(f[x][1]\)

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

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

也可以通过\(f[3]+f[2]+val_x\)构成。

\(f[x][2]=\max\limits_{y\in son_x}f[y][2]+val_x\)

\(f[x][3]=f[p][2]+f[q][1]+val_x\)

至于\(p\)\(q\)的选择,可以只记录\(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])\)

\(f\)\(g\)可以一遍普通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 @ 2021-03-30 16:27  Troverld  阅读(62)  评论(0编辑  收藏  举报