CF633F The Chocolate Spree
LVI.CF633F The Chocolate Spree
奇奇怪怪的直径题
思路1.用多种东西拼出来直径
我们设表示:
:子树内一条路径的最大值
:子树内两条路径的最大值
:子树内一条路径,且起点为的最大值
:子树内两条路径,且有一条起点为的最大值
则答案为。
考虑如何转移。
设为的儿子集合。
则:
:
可以是子树的最大值;
可以通过子树里面一个,再加上()拼出的一条路径构成;
也可以通过构成。
至于和的选择,可以只记录,和前三大的值,也可以直接偷懒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.二次扫描+换根
因为这两条路径一定会被一条边分成两半,两条路径各在一半里面,所以可以换根换出最大的断边。
设表示的子树中,以为起点的路径的最大值
设表示子树的直径。
设表示除了子树外的其余部分,以的父亲为起点的路径最大值
设表示除了子树外剩余部分的直径。
则答案为。
和可以一遍普通DP就能算出来;
我们设集合表示在的儿子中从大到小排序后的集合,
集合表示在的儿子中从大到小排序后的集合,
则可以从父亲边选一条,儿子边选一条;或者选两条儿子边;或者继承父亲的或儿子的。
即:
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[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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?