【bzoj2870】最长道路tree
foreverlasting · · 个人记录
题面
边分治模板题。
看到这道题,第一反应可能会是点分治。但实际上,点分治显然是有问题的。如果选择了点分治,那我们应该如何统计答案呢?因为这里需要的是点权最小与边权和的乘积,选择点分治的话无疑很难去处理点权最小的这个问题。于是,这里我们选择了边分治。这样的话,我们可以轻松通过查找边两端的路径信息,按点权大小排序后扫描一遍做到统计答案。
但是,直接边分治复杂度是错误的。在一个菊花树中,我们的边分治将会退化到
这里我们把虚点链接的边权设为0,这样虚点就不会被统计到了。
总复杂度
code:
//2018.8.24 by ljz
#include<bits/stdc++.h>
using namespace std;
#define res register LL
#define LL long long
#define inf 0x3f3f3f3f
#define eps 1e-15
inline LL read(){
res s=0;
bool w=0;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=1;ch=getchar();}
while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
return w?-s:s;
}
inline void _swap(res &x,res &y){
x^=y^=x^=y;
}
inline LL _abs(const res &x){
return x>0?x:-x;
}
inline LL _max(const res &x,const res &y){
return x>y?x:y;
}
inline LL _min(const res &x,const res &y){
return x<y?x:y;
}
const LL N=1e5+10;
namespace MAIN{
LL n,pn;
LL va[N];
LL ans;
namespace Tree{
struct E{
LL next,to;
E() {}
E(res next,res to):next(next),to(to) {}
}edge[N<<1];
LL head[N],cnt;
inline void init(){
memset(head,-1,sizeof(head));
}
inline void addedge(res u,res v){
edge[++cnt]=E(head[u],v),head[u]=cnt;
edge[++cnt]=E(head[v],u),head[v]=cnt;
}
}
namespace Edtree{
struct E{
LL next,to;
bool tag;
E() {}
E(res next,res to,bool tag):next(next),to(to),tag(tag) {}
}edge[N<<4];
LL head[N],cnt;
inline void init(){
memset(head,-1,sizeof(head));
}
inline void addedge(res u,res v,bool tag){
edge[++cnt]=E(head[u],v,tag),head[u]=cnt;
edge[++cnt]=E(head[v],u,tag),head[v]=cnt;
}
void build(res x,res fax){
res X=x;
bool flag=0;
for(res i=Tree::head[x];~i;i=Tree::edge[i].next){
res tox=Tree::edge[i].to;
if(tox==fax)continue;
if(flag)addedge(x,++n,0),va[n]=va[x],x=n;
addedge(x,tox,1);
build(tox,X);
flag=1;
}
}
bool vis[N];
LL siz[N],rt,w;
void getroot(res x,res fax,res sum){
siz[x]=1;
for(res i=head[x];~i;i=edge[i].next){
res tox=edge[i].to;
if(tox==fax||vis[i>>1])continue;
getroot(tox,x,sum);
siz[x]+=siz[tox];
res K=_max(siz[tox],sum-siz[tox]);
if(K<w)w=K,rt=i;
}
}
struct P{
LL va,len;
P() {}
P(res va,res len):va(va),len(len) {}
inline bool operator < (const P &b) const {
return va>b.va;
}
};
vector<P> vec[2];
void dfs(res x,res fax,res Va,res len,res sig){
vec[sig].push_back(P(Va,len));
for(res i=head[x];~i;i=edge[i].next){
res tox=edge[i].to;
if(tox==fax||vis[i>>1])continue;
dfs(tox,x,_min(Va,va[tox]),len+edge[i].tag,sig);
}
}
inline void calc(){
vec[0].clear(),vec[1].clear();
dfs(edge[rt].to,0,_min(va[edge[rt].to],inf),0,0),dfs(edge[rt^1].to,0,_min(va[edge[rt^1].to],inf),0,1);
sort(vec[0].begin(),vec[0].end()),sort(vec[1].begin(),vec[1].end());
for(res i=0,j=0,L=0;i<vec[0].size();i++){
while(j<vec[1].size()&&vec[0][i].va<=vec[1][j].va)L=_max(L,vec[1][j].len),j++;
if(j)ans=_max(ans,vec[0][i].va*(L+edge[rt].tag+vec[0][i].len+1));
}
for(res i=0,j=0,L=0;i<vec[1].size();i++){
while(j<vec[0].size()&&vec[1][i].va<=vec[0][j].va)L=_max(L,vec[0][j].len),j++;
if(j)ans=_max(ans,vec[1][i].va*(L+edge[rt].tag+vec[1][i].len+1));
}
}
void solve(res x,res sum){
w=inf;
getroot(x,0,sum);
if(w==inf)return;
vis[rt>>1]=1;
calc();
res now=rt,Sum=siz[edge[now].to];
solve(edge[now].to,Sum);
solve(edge[now^1].to,sum-Sum);
}
}
inline void MAIN(){
Tree::init();
Edtree::init();
n=read();
for(res i=1;i<=n;i++)va[i]=read();
for(res i=1;i<n;i++){
res u=read(),v=read();
Tree::addedge(u,v);
}
Edtree::cnt=1;
Edtree::build(1,0);
Edtree::solve(1,n);
printf("%Ld\n",ans);
}
}
int main(){
MAIN::MAIN();
return 0;
}