code
_Seg_Wick_Tree_ · · 个人记录
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+10;
int n,m;
int vis[maxn];
int ans[maxn];
int dep[maxn],fa[maxn],siz[maxn],son[maxn];
int dfn,top[maxn],id[maxn];
ll inf=1e18;
ll f[maxn];
struct seg{
ll val,lzy;
ll Left,Right;
}tree[maxn*4];
struct edge{
int u,v,w,id;
}s[maxn];
vector<int> G[maxn],d[maxn];
bool cmp(edge a,edge b){
return a.w<b.w;
}
int find(int x){
if(f[x]==x) return x;
return f[x]=find(f[x]);
}
ll kruscal(){
ll res=0,ans=0;
sort(s+1,s+m+1,cmp);
for(int i=1;i<=m;i++){
int x=find(s[i].u),y=find(s[i].v);
if(x!=y){
f[x]=y;
vis[i]=1;
G[s[i].u].push_back(s[i].v);
G[s[i].v].push_back(s[i].u);
ans++;
res+=s[i].w;
}
}
if(ans!=n-1) return -1;
else return res;
}
void pushup(int u){
tree[u].val=min(tree[u<<1].val,tree[u<<1|1].val);
return;
}
void dfs1(int u,int f){
fa[u]=f;
siz[u]=1;
dep[u]=dep[f]+1;
int s=0;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==f) continue;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[v]>s){
s=siz[v];
son[u]=v;
}
}
//cout << u << " " << fa[u] << " " << siz[u] << " " << dep[u] << " " << son[u] << "\n";
return;
}
void dfs2(int u,int f){
top[u]=f;
id[u]=++dfn;
if(son[u]) dfs2(son[u],f);
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa[u]||v==son[u]) continue;
dfs2(v,v);
}
return;
}
void build(int u,int L,int R){
tree[u].Left=L;
tree[u].Right=R;
tree[u].val=inf;
tree[u].lzy=-1;
if(L==R) return;
int mid=(L+R)>>1;
build(u<<1,L,mid);
build(u<<1|1,mid+1,R);
return;
}
bool inr(int L,int R,int l,int r){
return (L>=l)&&(R<=r);
}
bool otr(int L,int R,int l,int r){
return (R<l)||(L>r);
}
void maketag(int u,ll k){
if(k==-1) return;
tree[u].val=min(tree[u].val,k);
tree[u].lzy=min(tree[u].val,k);
return;
}
void pushdown(int u){
maketag(u<<1,tree[u].lzy);
maketag(u<<1|1,tree[u].lzy);
tree[u].lzy=-1;
return;
}
void update(int u,int l,int r,ll k){
if(inr(tree[u].Left,tree[u].Right,l,r)){
maketag(u,k);
return;
}else if(!otr(tree[u].Left,tree[u].Right,l,r)){
pushdown(u);
update(u<<1,l,r,k);
update(u<<1|1,l,r,k);
pushup(u);
}
return;
}
ll query(int u,int l,int r){
//cout << u << " " << l << " " << r << "\n";
//cout << tree[u].Left << " " << tree[u].Right << " " << tree[u].val << " awa\n";
if(inr(tree[u].Left,tree[u].Right,l,r)){
//cout << tree[u].Left << " " << tree[u].Right << " " << tree[u].val << " OK\n";
return tree[u].val;
}else if(!otr(tree[u].Left,tree[u].Right,l,r)){
pushdown(u);
ll a=query(u<<1,l,r),b=query(u<<1|1,l,r);
ll res=min(a,b);
//cout << a << " " << b << " " << tree[u].Left << " " << tree[u].Right << " " << res << " qsrwb\n";
return res;
}else{
//cout << tree[u].Left << " " << tree[u].Right << " " << tree[u].val << " unOK\n";
return inf;
}
}
void uptpath(int u,int v,int w){
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]) swap(u,v);
update(1,id[top[u]],id[u],w);
u=fa[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
if(u!=v){
update(1,id[son[u]],id[v],w);
}
return;
}
int main(){
cin >> n >> m;
for(int i=1;i<=m;i++){
cin >> s[i].u >> s[i].v >> s[i].w;
s[i].id=i;
}
for(int i=1;i<=n;i++) f[i]=i;
ll res=kruscal();
//cout << res << "\n";
dfs1(1,0);
dfs2(1,1);
build(1,1,n);
for(int i=1;i<=m;i++){
if(vis[i]) continue;
//cout << s[i].u << " " << s[i].v << " " << s[i].w << " check\n";
uptpath(s[i].u,s[i].v,s[i].w);
}
for(int i=1;i<=m;i++){
if(!vis[i]){
ans[s[i].id]=res;
continue;
}
int x;
//cout << s[i].u << " " << s[i].v << " " << dep[s[i].u] << " " << dep[s[i].v] << " rp++ \n";
if(dep[s[i].u]>dep[s[i].v]) x=s[i].u;
else x=s[i].v;
ll k=query(1,id[x],id[x]);
//cout << i << " " << x << " " << k << " " << s[i].w << " RP++ \n";
if(k==inf) ans[s[i].id]=-1;
else ans[s[i].id]=res+k-s[i].w;
}
for(int i=1;i<=m;i++) cout << ans[i] << "\n";
return 0;
}