题解:P5642 人造情感(emotion)
Imerance1018 · · 题解
没有黑吧。
令
考虑使用换根 DP 计算只考虑
接下来思考
此时难以判断
最后考虑答案怎么求,考虑把
视
::::success[Code]
#include<bits/stdc++.h>
#define pb emplace_back
#define int long long
#define all(v) v.begin(),v.end()
#define clz __builtin_clz
#define lb(x) ((x) & -(x))
#define lg(x) (clz(1)-clz(x))
#define rep(i,a,b) for(int i=(a);i<=(int)(b);i++)
using namespace std;
const int N=3e5+10,mod=998244353;
void add(int &x,int y){x+=y; if(x>=mod) x-=mod;}
int n,m;
vector<int> G[N];
struct BIT
{
int tr[N];
void init(){rep(i,1,n) tr[i]=0;}
void upd(int x,int v){for(;x<=n;x+=lb(x)) tr[x]+=v;}
void rev(int l,int r,int v){upd(l,v),upd(r+1,-v);}
int get(int x){int res=0; for(;x;x-=lb(x)) res+=tr[x]; return res;}
}t1,t2,t3;
int f[N],g[N],h[N];
int idx,dfn[N],rnk[N],sz[N],fa[N],st[20][N],dep[N];
int hs[N],top[N];
int dp1[N],dp2[N];
void pdfs(int x,int f)
{
hs[x]=-1;
sz[x]=1,fa[x]=f,dep[x]=dep[f]+1;
for(auto y:G[x])
if(y!=f)
{
pdfs(y,x),add(dp1[x],1ll*sz[x]*sz[y]%mod),sz[x]+=sz[y];
if(hs[x]==-1||sz[hs[x]]<sz[y]) hs[x]=y;
}
}
void podfs(int x,int t)
{
dfn[x]=++idx,rnk[dfn[x]]=x,st[0][dfn[x]]=x,top[x]=t;
if(hs[x]==-1) return;
podfs(hs[x],t);
for(auto y:G[x]) if(y!=fa[x]&&y!=hs[x]) podfs(y,y);
}
int mind(int x,int y){return (dep[x]<dep[y])?x:y;}
int lca(int x,int y)
{
if(x==y) return x;
x=dfn[x],y=dfn[y];
if(x>y) swap(x,y);
x++;
int cx=lg(y-x+1);
return fa[mind(st[cx][x],st[cx][y-(1<<cx)+1])];
}
vector<tuple<int,int,int>> opt[N];
int vd1[N];
void dfs1(int x)
{
g[x]=vd1[x]=0;
for(auto y:G[x])
if(y!=fa[x])
{
dfs1(y);
vd1[x]+=f[y],t1.rev(dfn[x],dfn[x]+sz[x]-1,f[y]);
g[x]+=f[y];
}
f[x]=g[x];
for(auto [u,v,w]:opt[x])
{
int res1=t1.get(dfn[u])+t1.get(dfn[v])-2*t1.get(dfn[x])+vd1[x],res2=t2.get(dfn[u])+t2.get(dfn[v])-2*t2.get(dfn[x]);
f[x]=max(f[x],res1-res2+w);
}
t2.rev(dfn[x],dfn[x]+sz[x]-1,f[x]);
}
#define lc x<<1
#define rc x<<1|1
#define mid ((l+r)>>1)
struct SGT
{
int tag[N<<2];
void revise(int x,int l,int r,int L,int R,int v)
{
if(L<=l&&r<=R) return tag[x]=max(tag[x],v),void();
if(L<=mid) revise(lc,l,mid,L,R,v);
if(mid+1<=R) revise(rc,mid+1,r,L,R,v);
}
int get(int x,int l,int r,int p)
{
if(l==r) return tag[x];
return max(tag[x],(p<=mid)?get(lc,l,mid,p):get(rc,mid+1,r,p));
}
}tr;
#undef lc
#undef rc
#undef mid
int smax[N],cmax[N],pos[N];
void chk(int x,int v,int p)
{
if(smax[x]<v)
{
if(p!=pos[x]) cmax[x]=smax[x];
pos[x]=p,smax[x]=v;
}
else if(cmax[x]<v&&p!=pos[x]) cmax[x]=v;
}
void modif(int x,int y,int v)
{
chk(x,v,-1),chk(y,v,-1);
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
if(dfn[top[x]]<dfn[x]) tr.revise(1,1,n,dfn[top[x]],dfn[x]-1,v);
chk(fa[top[x]],v,top[x]);
x=fa[top[x]];
}
if(dfn[x]>dfn[y]) swap(x,y);
if(dfn[x]<dfn[y]) tr.revise(1,1,n,dfn[x],dfn[y]-1,v);
}
void dfs2(int x)
{
for(auto y:G[x])
{
if(y==fa[x]) continue;
int cur=0;
if(y==hs[x]) cur=smax[x];
else cur=max(tr.get(1,1,n,dfn[x]),(pos[x]==y?cmax[x]:smax[x]));
h[y]=max(h[x]+g[x]-f[y],cur-f[y]);
dp2[y]=(dp1[x]+mod-sz[y]*(sz[x]-sz[y])%mod)%mod;
add(dp2[y],(sz[x]-sz[y])*(n-sz[x])%mod);
}
vector<tuple<int,int,int>> vls;
for(auto [u,v,w]:opt[x])
{
int res1=t1.get(dfn[u])+t1.get(dfn[v])-2*t1.get(dfn[x])+vd1[x],res2=t2.get(dfn[u])+t2.get(dfn[v])-2*t2.get(dfn[x]);
int cur=res1-res2+w+h[x];
modif(u,v,cur),vls.pb(cur,u,v);
}
sort(all(vls)),reverse(all(vls));
for(auto y:G[x]) if(y!=fa[x]) for(auto [w,u,v]:vls) if((dfn[u]<dfn[y]||dfn[u]>dfn[y]+sz[y]-1)&&(dfn[v]<dfn[y]||dfn[v]>dfn[y]+sz[y]-1)){h[y]=max(h[y],w-f[y]); break;}
for(auto y:G[x])
if(y!=fa[x]) dfs2(y);
}
signed main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>m;
rep(i,1,n-1)
{
int u,v; cin>>u>>v;
G[u].pb(v),G[v].pb(u);
}
pdfs(1,0),podfs(1,1);
rep(i,1,lg(n)) rep(j,1,n-(1<<i)+1) st[i][j]=mind(st[i-1][j],st[i-1][j+(1<<(i-1))]);
rep(i,1,m)
{
int u,v,w; cin>>u>>v>>w;
opt[lca(u,v)].pb(u,v,w);
}
dfs1(1),dfs2(1);
/*rep(i,1,n) cout<<f[i]<<" ";
cout<<"\n";*/
int ans=0;
rep(x,1,n)
{
for(auto y:G[x]) if(y!=fa[x]) add(ans,h[y]%mod*dp1[y]%mod);
add(ans,f[x]%mod*dp2[x]%mod);
}
ans=2*ans%mod;
rep(x,2,n) add(ans,h[x]%mod),add(ans,f[x]%mod);
ans=mod-ans;
add(ans,f[1]%mod*n%mod*n%mod);
cout<<ans<<"\n";
return 0;
}
::::