思路
题目就是让你在在重新定义权值的完全图上进行最小生成树。
考虑只将一些较优边作为备选边加入快乐的 kruskal。
对 tr2 进行点分治,那 可以通过 的相加得到。
将此次点分治区域的点在 tr1 上建棵虚树,点分治中的点 再连向 ,长度为 tr2 中的 ,此时 的权值为虚树上 。
用最短路算法处理出离每个节点最近的 状物和距离为 和 。
遍历虚树上每条边,将一条 放入备选边。
小证明一下。
假设 x 为父节点,y 为儿子节点, 即为连向某一子树中的最近 状物的距离,然后用 x 最近的 状物连向这些点使这这些子树连通。
不用管是否在根的不同子树。因为两点总会有分治到不在同一子树的时候,此时的权值一定比之前的优,可在 kruskal 中把之前的挤掉。
代码
最短路部分可用 spfa,开心。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5,inf=0x3f3f3f3f3f3f3f3f;
struct node{
int from,to,w,nxt;
}e[N*50];
int h[N],q[N],tot,n,e_cnt;
struct Tree1{
vector < node > G[N];
node edge[N<<2];
bool vis[N<<1];
int head[N<<1],pre[N<<1],dis[N<<1],fa[N][20],dfn[N],dep[N],dist[N],st[N],top,cnt;
inline bool cmp(int x,int y){
return dfn[x]<dfn[y];
}
inline void add(int x,int y,int z){
edge[++cnt]={x,y,z,head[x]};
head[x]=cnt;
}
inline void dfs(int x,int f){
fa[x][0]=f,dep[x]=dep[f]+1,dfn[x]=++cnt;
for (auto tmp:G[x]){
int v=tmp.to,w=tmp.w;
if (v==f) continue;
dist[v]=dist[x]+w,dfs(v,x);
}
}
inline int get_lca(int x,int y){
if (dep[x]<dep[y]) swap(x,y);
for (int i=19;i>=0;i--) if (dep[fa[x][i]]>=dep[y]) x=fa[x][i];
if (x==y) return x;
for (int i=19;i>=0;i--) if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
inline void build(){
cnt=0;
dfs(1,0);
memset(dis,0x3f,sizeof(dis));
for (int i=1;i<=19;i++) for (int x=1;x<=n;x++) fa[x][i]=fa[fa[x][i-1]][i-1];
}
inline void solve(){
cnt=0;
st[top=0]=1;
for (int i=1;i<=tot;i++) add(h[i],h[i]+n,q[i]),add(h[i]+n,h[i],q[i]);
sort(h+1,h+tot+1,[this](int x,int y){return dfn[x]<dfn[y];});
for (int i=1;i<=tot;i++){
if(h[i]==1) continue;
int x=h[i],LCA=get_lca(x,st[top]);
if (LCA!=st[top]){
while (dfn[st[top-1]]>dfn[LCA]){
add(st[top-1],st[top],dist[st[top]]-dist[st[top-1]]);
add(st[top],st[top-1],dist[st[top]]-dist[st[top-1]]);
top--;
}
if (dfn[st[top-1]]<dfn[LCA]){
add(LCA,st[top],dist[st[top]]-dist[LCA]);
add(st[top],LCA,dist[st[top]]-dist[LCA]);
st[top]=LCA;
}else{
add(LCA,st[top],dist[st[top]]-dist[LCA]);
add(st[top],LCA,dist[st[top]]-dist[LCA]);
top--;
}
}
st[++top]=x;
}
for (int i=0;i<top;i++){
add(st[i],st[i+1],dist[st[i+1]]-dist[st[i]]);
add(st[i+1],st[i],dist[st[i+1]]-dist[st[i]]);
}//建虚树
queue < int > q;
for (int i=1;i<=tot;i++) q.push(h[i]+n),pre[h[i]+n]=h[i]+n,dis[h[i]+n]=0;
while (q.size()){
int u=q.front();q.pop();vis[u]=0;
for (int i=head[u];i;i=edge[i].nxt){
int v=edge[i].to,w=edge[i].w;
if (dis[v]>dis[u]+w){
pre[v]=pre[u];
dis[v]=dis[u]+w;
if (!vis[v]) vis[v]=1,q.push(v);
}
}
}//最短路
for (int i=2;i<=cnt;i+=2){
int x=edge[i].from,y=edge[i].to,z=edge[i].w;
e[++e_cnt]={pre[x]-n,pre[y]-n,dis[x]+dis[y]+z};
}给kruakal加边
for (int i=1;i<=cnt;i++) dis[edge[i].to]=inf,vis[edge[i].to]=pre[edge[i].to]=head[edge[i].to]=0;
}
}tr1;
struct Tree2{
vector < node > G[N];
int siz[N],root,root_mx,dep[N];
bool vis[N];
inline void get_root(int x,int fa,int sum){
int mx=0;
siz[x]=1;
for (auto tmp:G[x]){
int v=tmp.to;
if (v==fa||vis[v]) continue;
get_root(v,x,sum),siz[x]+=siz[v],mx=max(mx,siz[v]);
}
mx=max(mx,sum-siz[x]);
if (mx<root_mx) root_mx=mx,root=x;
}
inline void dfs(int x,int fa){
h[++tot]=x,q[tot]=dep[x];
for (auto tmp:G[x]){
int v=tmp.to,w=tmp.w;
if (v==fa||vis[v]) continue;
dep[v]=dep[x]+w,dfs(v,x);
}
}
inline void solve(int x,int sum){
vis[x]=1,dep[x]=0,h[tot=1]=x;
for (auto tmp:G[x]){
if (vis[tmp.to]) continue;
dep[tmp.to]=dep[x]+tmp.w,dfs(tmp.to,x);
}
tr1.solve();
for (auto tmp:G[x]){
int v=tmp.to;
if (vis[v]) continue;
root_mx=inf,get_root(v,x,siz[v]>siz[x]?sum-siz[x]:siz[v]);
solve(root,siz[v]);
}
}//点分治板
}tr2;
inline bool cmp(node q,node w){
return q.w<w.w;
}
int fa[N];
inline int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
for (int i=1;i<n;i++){
int x,y,z;cin>>x>>y>>z;
tr1.G[x].push_back({0,y,z,0}),tr1.G[y].push_back({0,x,z,0});
}
for (int i=1;i<n;i++){
int x,y,z;cin>>x>>y>>z;
tr2.G[x].push_back({0,y,z,0}),tr2.G[y].push_back({0,x,z,0});
}
tr1.build();
tr2.root_mx=inf,tr2.get_root(1,0,n);
tr2.solve(tr2.root,n);
for (int i=1;i<=n;i++) fa[i]=i;
sort(e+1,e+e_cnt+1,cmp);
int ret=0;
for (int i=1;i<=e_cnt;i++){
int fx=find(e[i].from),fy=find(e[i].to),w=e[i].w;
if (fx==fy) continue;
fa[fx]=fy,ret+=w;
}//最短路板
cout<<ret;
return 0;
}