lzm点分治

· · 个人记录

点分治

实现详见笔记本。

题目

CF161D

纯模板。

:::info[code]

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=5e4+5;
ll n,k,x,y,sz[N],f[N],cnt[N],tmp[N],id,tot,rt,ans;
vector<ll>g[N];
void getsz(ll x,ll ls){
    sz[x]=1;
    for(auto i:g[x]){
        if(i==ls||f[i]) continue;
        getsz(i,x);
        sz[x]+=sz[i];
    }
}
void getrt(ll x,ll ls){
    ll mx=0;
    for(auto i:g[x]){
        if(i==ls||f[i]) continue;
        getrt(i,x);
        mx=max(mx,sz[i]);
    }
    mx=max(mx,tot-sz[x]);
    if(mx*2<=tot) rt=x;
}
void getmp(ll x,ll ls,ll d){
    if(d>k) return;
    tmp[++id]=d;
    for(auto i:g[x]){
        if(i==ls||f[i]) continue;
        getmp(i,x,d+1);
    }
}
void sol(ll x){
    getsz(x,0);
    tot=sz[x];
    getrt(x,0);
    cnt[0]=f[rt]=1;
    for(int i:g[rt]){
        if(f[i]) continue;
        id=0;
        getmp(i,rt,1);
        for(int j=1;j<=id;j++){
            ans+=cnt[k-tmp[j]];
        }
        for(int j=1;j<=id;j++){
            cnt[tmp[j]]++;
        }
    }
    for(int i=0;i<=k;i++){
        cnt[i]=0;
    }
    for(int i:g[rt]){
        if(f[i]) continue;
        sol(i);
    }
}
int main(){
    cin>>n>>k;
    for(int i=1;i<n;i++){
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    sol(1);
    cout<<ans;
    return 0;
}

:::

P4178

加上边权,用树状数组求前缀和。

:::info[code]

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lb x&(-x)
const int N=4e4+5;
ll n,k,x,y,w,sz[N],f[N],cnt[N],tmp[N],tr[N],id,tot,rt,ans;
struct P{
    ll x,w;
};
vector<P>g[N];
void add(ll x){
    while(x<=k+1){
        tr[x]++;
        x+=lb;
    }
}
ll ask(ll x){
    ll ans=0;
    while(x){
        ans+=tr[x];
        x-=lb;
    }
    return ans;
}
void getsz(ll x,ll ls){
    sz[x]=1;
    for(auto j:g[x]){
        int i=j.x;
        if(i==ls||f[i]) continue;
        getsz(i,x);
        sz[x]+=sz[i];
    }
}
void getrt(ll x,ll ls){
    ll mx=0;
    for(auto j:g[x]){
        int i=j.x;
        if(i==ls||f[i]) continue;
        getrt(i,x);
        mx=max(mx,sz[i]);
    }
    mx=max(mx,tot-sz[x]);
    if(mx*2<=tot) rt=x;
}
void getmp(ll x,ll ls,ll d){
    if(d>k) return;
    tmp[++id]=d;
    for(auto i:g[x]){
        if(i.x==ls||f[i.x]) continue;
        getmp(i.x,x,d+i.w);
    }
}
void sol(ll x){
    getsz(x,0);
    tot=sz[x];
    getrt(x,0);
    f[rt]=1;
    add(1);
    for(auto i:g[rt]){
        if(f[i.x]) continue;
        id=0;
        getmp(i.x,rt,i.w);
        for(int j=1;j<=id;j++){
            ans+=ask(k-tmp[j]+1);
        }
        for(int j=1;j<=id;j++){
            add(tmp[j]+1);
        }
    }
    for(int i=0;i<=k;i++){
        tr[i]=0;
    }
    for(auto i:g[rt]){
        if(f[i.x]) continue;
        sol(i.x);
    }
}
int main(){
    cin>>n;
    for(int i=1;i<n;i++){
        cin>>x>>y>>w;
        g[x].push_back({y,w});
        g[y].push_back({x,w});
    }
    cin>>k;
    sol(1);
    cout<<ans;
    return 0;
}

:::

CF1101D

要求路径上 \gcd>1,则每个数都有至少一个相同的质因子。枚举重心的每一个质因子,找子树中存在的最长次长链贡献。质因子个数不多,不会超时。

:::info[code]

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+5;
ll n,x,y,sz[N],f[N],a[N],s[N],p[N],tmp[N],l1[N],l2[N],id,tot,rt,ans;
vector<ll>g[N],d[N];
bool isp[N];
void init(int n){
    for(int i=2;i<=n;i++){
        if(isp[i]) continue;
        d[i].push_back(i);
        for(int j=i+i;j<=n;j+=i){
            isp[j]=1;
            d[j].push_back(i);
        }
    }
}
void getsz(ll x,ll ls){
    sz[x]=1;
    for(auto i:g[x]){
        if(i==ls||f[i]) continue;
        getsz(i,x);
        sz[x]+=sz[i];
    }
}
void getrt(ll x,ll ls){
    ll mx=0;
    for(auto i:g[x]){
        if(i==ls||f[i]) continue;
        getrt(i,x);
        mx=max(mx,sz[i]);
    }
    mx=max(mx,tot-sz[x]);
    if(mx*2<=tot) rt=x;
}
ll getmp(ll x,ll ls,ll d,ll s){
    ll ans=d;
    for(auto i:g[x]){
        if(i==ls||f[i]) continue;
        if(a[i]%s) continue;
        ans=max(ans,getmp(i,x,d+1,s));
    }
    return ans;
}
void sol(ll x){
    getsz(x,0);
    tot=sz[x];
    getrt(x,0);
    f[rt]=1;
    ll cnt=0;
    for(int j:d[a[rt]]){
        s[j]=++cnt;
        p[cnt]=j;
    }
    for(auto i:g[rt]){
        if(f[i]) continue;
        for(int j:d[a[i]]){
            if(!s[j]) continue;
            ll t=getmp(i,rt,1,j);
            if(l1[s[j]]<t) l2[s[j]]=l1[s[j]],l1[s[j]]=t;
            else l2[s[j]]=max(l2[s[j]],t);
        }
    }
    for(int j=1;j<=cnt;j++){
        ans=max(ans,l1[j]+l2[j]+1);
        s[p[j]]=l1[j]=l2[j]=0;
    }
    for(auto i:g[rt]){
        if(f[i]) continue;
        sol(i);
    }
}
int main(){
    cin>>n;
    init(2e5);
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<n;i++){
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    sol(1);
    cout<<ans;
    return 0;
}

:::

P10421

类似于 P4178。

要求 L\le tmp_j+len\le R。

转化:L-tmp_j\le len\le R-tmp_j。

用树状数组求出区间内链的个数 cnt 和总长度 sum,每条经过 rt 的链长 tmp_j+len,求一个 \sum 变为 cnt*tmp_j+sum,贡献。

初始插入单个 rt,记录插入树状数组的数对 \{1,tmp_j\},方便清空。

:::info[code]

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lb x&(-x)
const int N=1e6+5;
ll n,l,r,x,y,w,sz[N],f[N],cnt[N],tmp[N],tc[N],ts[N],id,tot,rt,ans;
vector<ll>g[N];
void add(ll x,ll c,ll s){
    x++;
    while(x<=n+1){
        tc[x]+=c;
        ts[x]+=s;
        x+=lb;
    }
}
ll askc(ll x){
    if(x<0) return 0;
    x++;
    ll ans=0;
    while(x){
        ans+=tc[x];
        x-=lb;
    }
    return ans;
}
ll asks(ll x){
    if(x<0) return 0;
    x++;
    ll ans=0;
    while(x){
        ans+=ts[x];
        x-=lb;
    }
    return ans;
}
void getsz(ll x,ll ls){
    sz[x]=1;
    for(auto i:g[x]){
        if(i==ls||f[i]) continue;
        getsz(i,x);
        sz[x]+=sz[i];
    }
}
void getrt(ll x,ll ls){
    ll mx=0;
    for(auto i:g[x]){
        if(i==ls||f[i]) continue;
        getrt(i,x);
        mx=max(mx,sz[i]);
    }
    mx=max(mx,tot-sz[x]);
    if(mx*2<=tot) rt=x;
}
void getmp(ll x,ll ls,ll d){
    tmp[++id]=d;
    for(auto i:g[x]){
        if(i==ls||f[i]) continue;
        getmp(i,x,d+1);
    }
}
void sol(ll x){
    getsz(x,0);
    tot=sz[x];
    getrt(x,0);
    f[rt]=1;
    add(0,1,0);
    ll p=0;
    cnt[++p]=0;
    for(auto i:g[rt]){
        if(f[i]) continue;
        id=0;
        getmp(i,rt,1);
        for(int j=1;j<=id;j++){
            ll x=l-tmp[j],y=r-tmp[j];
            ans+=tmp[j]*(askc(y)-askc(x-1))+asks(y)-asks(x-1);
        }
        for(int j=1;j<=id;j++){
            add(tmp[j],1,tmp[j]);
            cnt[++p]=tmp[j];
        }
    }
    for(int i=1;i<=p;i++){
        add(cnt[i],-1,-cnt[i]);
    }
    for(auto i:g[rt]){
        if(f[i]) continue;
        sol(i);
    }
}
int main(){
    cin>>n>>l>>r;
    for(int i=2;i<=n;i++){
        cin>>x;
        g[x].push_back(i);
        g[i].push_back(x);
    }
    sol(1);
    cout<<ans;
    return 0;
}

:::

20260817 测试

CF1575E

套用点分治模板思路。

一条经过根节点的路径,可以算出其一个子树中一条链的权值和 w,根节点到链端的换乘次数 x。

类似 P10421,用树状数组维护之前的总权值 s 和方案数 c,乍一看贡献为 s_{k-x}+(w+a_{rt})(c_{k-x}+1)(根节点到链端也算)。

注意到根节点两侧若颜色不同则额外需要一次换乘,因颜色只有两种,用两个树状数组维护 s1,s2,c1,c2。

根节点一侧颜色为 1 时贡献:

(s1_{k-x}+s2_{k-x-1})+(w+a_{rt})(c1_{k-x}+c2_{k-x-1}+1)

颜色为 0 时同理。

记得根节点自己也算长度为 0 的路径,有点权。

:::info[code]

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lb x&(-x)
const int N=2e5+5,M=1e9+7;
ll T,n,k,a[N],x,y,w,rt,tot,sz[N],ts1[N],tc1[N],ts2[N],tc2[N],f[N],id,p,ans;
struct P{
    ll x,w;
}tmp[N];
struct Q{
    ll c,x,w;
}d[N];
vector<P>g[N];
void add1(ll x,ll c,ll s){
    x++;
    while(x<=n+1){
        tc1[x]+=c;
        ts1[x]+=s;
        x+=lb;
    }
}
ll asks1(ll x){
    if(x<0) return 0;
    ll ans=0;
    x++;
    while(x){
        ans+=ts1[x];
        ans%=M;
        x-=lb;
    }
    return ans;
}
ll askc1(ll x){
    if(x<0) return 0;
    ll ans=0;
    x++;
    while(x){
        ans+=tc1[x];
        ans%=M;
        x-=lb;
    }
    return ans;
}
void add2(ll x,ll c,ll s){
    x++;
    while(x<=n+1){
        tc2[x]+=c;
        ts2[x]+=s;
        x+=lb;
    }
}
ll asks2(ll x){
    if(x<0) return 0;
    ll ans=0;
    x++;
    while(x){
        ans+=ts2[x];
        ans%=M;
        x-=lb;
    }
    return ans;
}
ll askc2(ll x){
    if(x<0) return 0;
    ll ans=0;
    x++;
    while(x){
        ans+=tc2[x];
        ans%=M;
        x-=lb;
    }
    return ans;
}
void getsz(ll x,ll ls){
    sz[x]=1;
    for(auto i:g[x]){
        if(i.x==ls||f[i.x]) continue;
        getsz(i.x,x);
        sz[x]+=sz[i.x];
    }
}
void getrt(ll x,ll ls){
    ll mx=0;
    for(auto i:g[x]){
        if(i.x==ls||f[i.x]) continue;
        getrt(i.x,x);
        mx=max(mx,sz[i.x]);
    }
    mx=max(mx,tot-sz[x]);
    if(mx*2<=tot) rt=x;
}
void getmp(ll x,ll ls,ll d,ll lc,ll s){
    if(d>k) return;
    tmp[++id]={d,s};
    for(auto i:g[x]){
        if(i.x==ls||f[i.x]) continue;
        getmp(i.x,x,d+(i.w!=lc),i.w,s+a[i.x]);
    }
}
void sol(ll x){
    getsz(x,-1);
    tot=sz[x];
    getrt(x,-1);
    f[rt]=1;
    p=0;
    ans=(ans+a[rt])%M;
//  add1(rt,1,a[rt]);
//  d[++p]={1,1,0};
//  add2(rt,1,a[rt]);
//  d[++p]={2,1,0};
    for(auto i:g[rt]){
        if(f[i.x]) continue;
        id=0;
        getmp(i.x,rt,0,i.w,a[i.x]);
        for(int j=1;j<=id;j++){
            ll d1,d2;
            if(i.w==1){
                d1=k-tmp[j].x;d2=k-tmp[j].x-1;
            }else{
                d1=k-tmp[j].x-1;d2=k-tmp[j].x;
            }
            ans+=asks1(d1)+asks2(d2)+(tmp[j].w+a[rt])*(askc1(d1)+askc2(d2)+1);
            ans%=M;
        }
        for(int j=1;j<=id;j++){
            if(i.w==1) add1(tmp[j].x,1,tmp[j].w);
            else add2(tmp[j].x,1,tmp[j].w);
            d[++p]={i.w,tmp[j].x,tmp[j].w};
        }
    }
    for(int i=1;i<=p;i++){
        if(d[i].c==1) add1(d[i].x,-1,-d[i].w);
        else add2(d[i].x,-1,-d[i].w);
    }
    for(auto i:g[rt]){
        if(f[i.x]) continue;
        sol(i.x);
    }
}
int main(){
//  freopen("as03.in","r",stdin);
//  freopen("as03.out","w",stdout);
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<n;i++){
        cin>>x>>y>>w;
        g[x].push_back({y,w});
        g[y].push_back({x,w});
    }
    sol(1);
    cout<<ans;
    return 0;
}

:::

20260818 测试

P6626

写了个在线,被卡常了。

我的思路:

点分治中找一个点经过重心不超过 \log 次。

点分治预处理,需要知道以 x 为端点经过 rt 且长度为 k 的链数量。求出 x 到 rt 的长度 d,cnt[k-d] 中包含了 y 与 x 同在 i 子树的情况(i 为 rt 的子节点,包含 x),去掉 tr[i][k-d] 即可。

离线:

把查询存起来,最后点分治中查到其中一个 x 就统计经过 rt 的路径数量。

:::info[code]

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+5;
ll T,n,m,x,y,rt,tot,f[N],sz[N];
unordered_map<ll,ll>cnt[N],tr[N];
struct P{
    ll x,y,w;
};
vector<P>q[N];
vector<ll>g[N];
void getsz(ll x,ll ls){
    sz[x]=1;
    for(int i:g[x]){
        if(i==ls||f[i]) continue;
        getsz(i,x);
        sz[x]+=sz[i];
    }
}
void getrt(ll x,ll ls){
    ll mx=0;
    for(int i:g[x]){
        if(i==ls||f[i]) continue;
        getrt(i,x);
        mx=max(mx,sz[i]);
    }
    mx=max(mx,tot-sz[x]);
    if(mx*2<=tot) rt=x;
}
void getmp(ll rt,ll t,ll x,ll ls,ll d){
    cnt[rt][d]++;
    tr[d][rt*n+t]++;
    q[x].push_back({rt,t,d});
    for(int i:g[x]){
        if(i==ls||f[i]) continue;
        getmp(rt,t,i,x,d+1);
    }
}
inline void sol(ll x){
    getsz(x,0);
    tot=sz[x];
    getrt(x,0);
    f[rt]=1;
    cnt[rt][0]=1;
    for(int i:g[rt]){
        if(f[i]) continue;
        getmp(rt,i,i,rt,1);
    }
    for(int i:g[rt]){
        if(f[i]) continue;
        sol(i);
    }
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>T;
    while(T--){
        cin>>n>>m;
        for(int i=0;i<=n;i++){
            g[i].clear();
            q[i].clear();
            cnt[i].clear();
            tr[i].clear();
            f[i]=0;
        }
        for(int i=1;i<n;i++){
            cin>>x>>y;
            g[x].push_back(y);
            g[y].push_back(x);
        }
        sol(1);
        while(m--){
            cin>>x>>y;
            ll ans=0;
            if(cnt[x].count(y)) ans=cnt[x][y];
            for(P i:q[x]){
                if(y-i.w<0) continue;
                if(cnt[i.x].count(y-i.w)) ans+=cnt[i.x][y-i.w];
                if(tr[y-i.w].count(i.x*n+i.y)) ans-=tr[y-i.w][i.x*n+i.y];
            }
            cout<<ans<<"\n";
        }
    }
    return 0;
}

:::

P12491

考虑点分治如何得到好路径。

设 rt~x 路径上最小 a 值 m1,路径权值和 s1,用于拼接的那一截 m2 和 s2,要求 \min(m1,m2)*(s1+s2-b_{rt})>=k。

若 min(m1,m2) 为定值,则问题就是找满足条件最小的 s2。

处理 rt 的子节点时,先维护所有最小 a 值,从大到小排序。每次枚举到的就是最小的 a。

对于确定的 m,问题转化为:

s2\ge \frac{i}{m}-s1+b_{rt}

用数据结构(如树状数组)维护某个 v 及以后的最小的 s2。

:::info[code]

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+5;
const ll INF=1e18;
ll n,k,x,y,sz[N],f[N],a[N],b[N],id,tot,rt,ans=INF;
vector<ll>g[N];
struct P{
    ll m,s,c;
}p[N];
bool cmp(P a,P b){
    return a.m>b.m;
}
struct Q{
    ll m1,c1,m2,c2;
}tr[N<<2];
void upd(Q &res,ll v,ll c){
    if(c==-1||v>=INF) return;
    if(v<res.m1){
        if(c!=res.c1) res.m2=res.m1,res.c2=res.c1;
        res.m1=v,res.c1=c;
    }else if(c!=res.c1&&v<res.m2){
        res.m2=v,res.c2=c;
    }
}
Q merge(Q A,Q B){
    Q res={INF,-1,INF,-1};
    upd(res,A.m1,A.c1);
    upd(res,A.m2,A.c2);
    upd(res,B.m1,B.c1);
    upd(res,B.m2,B.c2);
    return res;
}
void build(ll id,ll l,ll r){
    tr[id]={INF,-1,INF,-1};
    if(l==r) return;
    ll mid=l+r>>1;
    build(id*2,l,mid);
    build(id*2+1,mid+1,r);
}
void upd(ll id,ll l,ll r,ll x,ll v,ll c){
    if(l==r){
        upd(tr[id],v,c);
        return;
    }
    ll mid=l+r>>1;
    if(x<=mid) upd(id*2,l,mid,x,v,c);
    else upd(id*2+1,mid+1,r,x,v,c);
    tr[id]=merge(tr[id*2],tr[id*2+1]);
}
Q ask(ll id,ll l,ll r,ll cl,ll cr){
    if(cl<=l&&r<=cr) return tr[id];
    ll mid=l+r>>1;
    if(cr<=mid) return ask(id*2,l,mid,cl,cr);
    if(cl>mid) return ask(id*2+1,mid+1,r,cl,cr);
    return merge(ask(id*2,l,mid,cl,cr),ask(id*2+1,mid+1,r,cl,cr));
}
void getsz(ll x,ll ls){
    sz[x]=1;
    for(auto i:g[x]){
        if(i==ls||f[i]) continue;
        getsz(i,x);
        sz[x]+=sz[i];
    }
}
void getrt(ll x,ll ls){
    ll mx=0;
    for(auto i:g[x]){
        if(i==ls||f[i]) continue;
        getrt(i,x);
        mx=max(mx,sz[i]);
    }
    mx=max(mx,tot-sz[x]);
    if(mx*2<=tot) rt=x;
}
void getmp(ll x,ll ls,ll mi,ll sum,ll c){
    p[++id]={mi,sum,c};
    for(auto i:g[x]){
        if(i==ls||f[i]) continue;
        getmp(i,x,min(mi,a[i]),sum+b[i],c);
    }
}
void sol(ll x){
    getsz(x,0);
    tot=sz[x];
    getrt(x,0);
    f[rt]=1;
    id=0;
    p[++id]={a[rt],b[rt],0};
    for(auto i:g[rt]){
        if(f[i]) continue;
        getmp(i,rt,min(a[rt],a[i]),b[rt]+b[i],i);
    }
    vector<ll>v;
    for(int i=1;i<=id;i++) v.push_back(p[i].s);
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    ll ms=v.size();
    build(1,1,ms);
    sort(p+1,p+id+1,cmp);
    for(int i=1;i<=id;i++){
        ll rs=(k+p[i].m-1)/p[i].m+b[rt]-p[i].s;
        ll pos=lower_bound(v.begin(),v.end(),rs)-v.begin()+1;
        if(pos<=ms){
            Q t=ask(1,1,ms,pos,ms);
            if(t.c1!=p[i].c&&t.m1<INF){
                ans=min(ans,p[i].s+t.m1-b[rt]);
            }else if(t.c2!=p[i].c&&t.m2<INF){
                ans=min(ans,p[i].s+t.m2-b[rt]);
            }
        }
        ll id=lower_bound(v.begin(),v.end(),p[i].s)-v.begin()+1;
        upd(1,1,ms,id,p[i].s,p[i].c);
    }
    for(auto i:g[rt]){
        if(f[i]) continue;
        sol(i);
    }
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i]>>b[i];
        if(a[i]*b[i]>=k) ans=min(ans,b[i]);
    }
    for(int i=1;i<n;i++){
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    sol(1);
    cout<<ans<<"\n";
    return 0;
}

:::

AT_cf17_final_j

我们删掉不可能用的边。环上边权最大的边一定不可用。 已知 $f(x,y)=a_x+dis(x,y)+a_y$,点分治中 $f(x,y)=a_x+dis(x,rt)+dis(rt,y)+a_y$。 定义 $s_x=dis(rt,x)+a_x$,则 $f(x,y)=s_x+s_y$。 我们找到点 $p$ 使得 $s_p=\min(s)$,让 $rt$ 内其余点与它连边就够了。再多连一条边会形成一个环,由于 $p$ 已经是最小,这条边一定会成为环上边权最大的边。 这样总边数只有 $n\log n$。 :::info[code] ```cpp #include <bits/stdc++.h> using namespace std; #define ll long long const int N=4e6+5; ll T,n,m,x,y,w,rt,tot,f[N],sz[N],a[N],fa[N],id,cnt,ans; ll fd(ll x){ if(fa[x]==x) return x; return fa[x]=fd(fa[x]); } struct P{ ll x,w; }tmp[N]; bool cmp(P a,P b){ return a.w<b.w; } struct Q{ ll x,y,w; }p[N]; bool cmq(Q a,Q b){ return a.w<b.w; } vector<P>g[N]; void getsz(ll x,ll ls){ sz[x]=1; for(auto i:g[x]){ if(i.x==ls||f[i.x]) continue; getsz(i.x,x); sz[x]+=sz[i.x]; } } void getrt(ll x,ll ls){ ll mx=0; for(auto i:g[x]){ if(i.x==ls||f[i.x]) continue; getrt(i.x,x); mx=max(mx,sz[i.x]); } mx=max(mx,tot-sz[x]); if(mx*2<=tot) rt=x; } inline void getmp(ll x,ll ls,ll d){ tmp[++id]={x,d+a[x]}; for(auto i:g[x]){ if(i.x==ls||f[i.x]) continue; getmp(i.x,x,d+i.w); } } inline void add(ll x,ll ls,ll d,ll c,ll w){ if(x!=c) p[++cnt]={x,c,d+w+a[x]}; for(auto i:g[x]){ if(i.x==ls||f[i.x]) continue; add(i.x,x,d+i.w,c,w); } } inline void sol(ll x){ getsz(x,0); tot=sz[x]; getrt(x,0); f[rt]=1; id=0; tmp[++id]={rt,a[rt]}; for(auto i:g[rt]){ if(f[i.x]) continue; getmp(i.x,rt,i.w); } sort(tmp+1,tmp+id+1,cmp); add(rt,0,0,tmp[1].x,tmp[1].w); for(auto i:g[rt]){ if(f[i.x]) continue; sol(i.x); } } void dfs(ll s,ll x,ll ls,ll sum){ for(P i:g[x]){ if(i.x==ls) continue; dfs(s,i.x,x,sum+i.w); if(s<i.x) p[++cnt]={s,i.x,a[s]+sum+i.w+a[i.x]}; } } int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; fa[i]=i; } for(int i=1;i<n;i++){ cin>>x>>y>>w; g[x].push_back({y,w}); g[y].push_back({x,w}); } sol(1); sort(p+1,p+cnt+1,cmq); for(int j=1;j<=cnt;j++){ Q i=p[j]; if(fd(i.x)==fd(i.y)) continue; fa[fd(i.x)]=fd(i.y); ans+=i.w; } cout<<ans; return 0; } ``` :::