。。。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define ppp pair<int,pair<int,ll> > 5 #define X first 6 #define Y second 7 const int N=1e5+5; 8 const ll inf=1e17; 9 int n,m,cc,T,tt,nn,tp,dep[N],vis[N],lg[N<<1],f[N<<1][20],dfn[N],t[N],st[N],hd[N],rt[N],ls[N*40],rs[N*40]; 10 ll ans,d[N],mx1[N*40],mx2[N*40]; 11 vector<int>g[N];vector<ppp>v[N]; 12 struct E{int v,nxt;ll w;}e[N<<1]; 13 inline void add(int u,int v,ll w){e[++cc]=(E){v,hd[u],w};hd[u]=cc;} 14 void dfs(int x){f[++T][0]=x;dfn[x]=T;for(int i=hd[x];i;i=e[i].nxt){int y=e[i].v;dep[y]=dep[x]+1;d[y]=d[x]+e[i].w;dfs(y);f[++T][0]=x;}} 15 inline int mn(int u,int v){return dep[u]<dep[v]?u:v;} 16 inline int lca(int u,int v){u=dfn[u];v=dfn[v];if(u>v)swap(u,v);int d=lg[v-u+1];return mn(f[u][d],f[v-(1<<d)+1][d]);} 17 inline ll dis(int u,int v){return d[u]+d[v]-d[lca(u,v)]*2;} 18 inline int nwnd(){int x=++tt;ls[x]=rs[x]=0;mx1[x]=mx2[x]=-inf;return x;} 19 inline void up(int x){mx1[x]=max(mx1[ls[x]],mx1[rs[x]]);mx2[x]=max(mx2[ls[x]],mx2[rs[x]]);} 20 void upd(int&x,int l,int r,int p,ll v1,ll v2,ll d) 21 { 22 if(!x)x=nwnd();mx1[x]=max(mx1[x],v1);mx2[x]=max(mx2[x],v2);if(l==r)return; 23 int mid=l+r>>1;if(p<=mid){ans=max(ans,v1+mx2[rs[x]]-d);upd(ls[x],l,mid,p,v1,v2,d);} 24 else {ans=max(ans,v2+mx1[ls[x]]-d);upd(rs[x],mid+1,r,p,v1,v2,d);} 25 } 26 int merge(int x,int y,int l,int r,ll d) 27 { 28 if(!x||!y)return x+y;int mid=l+r>>1; 29 if(l==r){mx1[x]=max(mx1[x],mx1[y]);mx2[x]=max(mx2[x],mx2[y]);return x;} 30 ans=max(ans,mx1[ls[x]]+mx2[rs[y]]-d);ans=max(ans,mx1[ls[y]]+mx2[rs[x]]-d); 31 ls[x]=merge(ls[x],ls[y],l,mid,d);rs[x]=merge(rs[x],rs[y],mid+1,r,d);up(x);return x; 32 } 33 void cut(int&x,int l,int r,int p) 34 { 35 if(!x)return;if(l==r){x=0;return;}int mid=l+r>>1; 36 if(p<=mid)rs[x]=0,cut(ls[x],l,mid,p);else cut(rs[x],mid+1,r,p);up(x); 37 } 38 void dfs1(int x){for(int i=hd[x],y;i;i=e[i].nxt)dfs1(y=e[i].v),rt[x]=merge(rt[x],rt[y],1,n,d[x]);cut(rt[x],1,n,dep[x]-1);} 39 struct dia 40 { 41 int u[2];ll v[2],l;dia(){u[0]=u[1]=0;v[0]=v[1]=l=-inf;} 42 dia(int x,ll w){u[0]=x;v[0]=w;u[1]=0;v[1]=-inf;l=-inf;} 43 }tr[N]; 44 inline bool cmp(ppp x,ppp y){return dfn[x.X]<dfn[y.X];} 45 inline dia merge(dia x,dia y,ll d) 46 { 47 dia r;ll w;r=x.l>y.l?x:y; 48 for(int i=0;i<2;i++)if(x.u[i])for(int j=0;j<2;j++)if(y.u[j]) 49 { 50 w=dis(x.u[i],y.u[j])+x.v[i]+y.v[j];ans=max(ans,(w-d)/2); 51 if(w>r.l){r.u[0]=x.u[i];r.u[1]=y.u[j];r.v[0]=x.v[i];r.v[1]=y.v[j];r.l=w;} 52 } 53 return r; 54 } 55 void dfss(int x,int rt){for(int i=0;i<g[x].size();i++){int y=g[x][i];dfss(y,rt);if(x!=rt)tr[x]=merge(tr[x],tr[y],d[x]*2);}vis[x]=0;g[x].clear();} 56 inline void ins(int x) 57 { 58 int y=lca(x,st[tp]);if(!vis[y]){vis[y]=1;t[++nn]=y;} 59 while(tp>1&&dep[st[tp-1]]>=dep[y])g[st[tp-1]].push_back(st[tp]),tp--; 60 if(st[tp]!=y)g[y].push_back(st[tp]),st[tp]=y;st[++tp]=x; 61 } 62 void sol() 63 { 64 scanf("%d",&n);ans=-inf;cc=T=tt=0;dep[1]=1; 65 for(int i=1;i<=n;i++)hd[i]=0,rt[i]=0,v[i].clear(); 66 for(int i=1;i<n;i++){int u,v;ll w;scanf("%d%d%lld",&u,&v,&w);add(u,v,w);} 67 dfs(1);scanf("%d",&m);for(int i=2;i<=T;i++)lg[i]=lg[i>>1]+1; 68 for(int j=1;j<=18;j++)for(int i=1;i+(1<<j)-1<=T;i++)f[i][j]=mn(f[i][j-1],f[i+(1<<j-1)][j-1]); 69 for(int i=1;i<=m;i++) 70 { 71 int x,y,t;ll z;scanf("%d%d%lld",&x,&y,&z);t=lca(x,y);ll w=dis(x,y)-z; 72 if(x!=t)upd(rt[x],1,n,dep[t],w,w+d[t],d[x]),v[t].push_back(make_pair(x,make_pair(y,w+d[x]-z))); 73 if(y!=t)upd(rt[y],1,n,dep[t],w,w+d[t],d[y]),v[t].push_back(make_pair(y,make_pair(x,w+d[y]-z))); 74 } 75 dfs1(1); 76 for(int i=1;i<=n;i++) 77 { 78 sort(v[i].begin(),v[i].end(),cmp);tp=nn=0;st[++tp]=i;vis[i]=1;t[++nn]=i; 79 for(int j=0;j<v[i].size();j++)if(!vis[v[i][j].X]){vis[v[i][j].X]=1;t[++nn]=v[i][j].X;ins(v[i][j].X);} 80 while(tp>1){g[st[tp-1]].push_back(st[tp]);tp--;} 81 for(int j=1;j<=nn;j++){int x=t[j];tr[x].u[0]=tr[x].u[1]=0;tr[x].v[0]=tr[x].v[1]=tr[x].l=-inf;} 82 for(int j=0;j<v[i].size();j++){dia x=dia(v[i][j].Y.X,v[i][j].Y.Y);tr[v[i][j].X]=merge(tr[v[i][j].X],x,d[v[i][j].X]*2);} 83 dfss(i,i); 84 } 85 if(ans<=-1e16)puts("F");else printf("%lld ",ans); 86 } 87 int main() 88 { 89 freopen("center.in","r",stdin);freopen("center.out","w",stdout); 90 mx1[0]=mx2[0]=-inf;int T;scanf("%d",&T);while(T--)sol();return 0; 91 }