Connected undirected weighted graph without self-loops and multiple edges is given. Graph contains n vertices and m edges.
For each edge (u, v) find the minimal possible weight of the spanning tree that contains the edge (u, v).
The weight of the spanning tree is the sum of weights of all edges included in spanning tree.
Input
First line contains two integers n and m (1 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105) — the number of vertices and edges in graph.
Each of the next m lines contains three integers ui, vi, wi (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ wi ≤ 109) — the endpoints of the i-th edge and its weight.
Output
Print m lines. i-th line should contain the minimal possible weight of the spanning tree that contains i-th edge.
The edges are numbered from 1 to m in order of their appearing in input.
Examples
5 7
1 2 3
1 3 1
1 4 5
2 3 2
2 5 3
3 4 2
4 5 4
9
8
11
8
8
8
9 次小生成树模板:
lca+倍增+最小生成树
#define eps 1e-6
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<algorithm>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int N = ;
//*************************
int n, m;
//kruscal
struct tree
{
int a,b,w;
} p[N],s[N];
int p1[N];
int rk1[N];
//倍增
int fa[N][], max_e[N][], dep[N];
//lca
struct edge
{
int to, nxt,w;
} e[N<<];
int fst[N], tot;
struct query
{
int to,nxt;
int idx;
} Q[N<<];
int h[N],tt;
int p2[N],rk2[N];
int acr[N], ans[N];
bool vis[N];
//************************
void CLS()
{
tot = ;
memset(fst,-,sizeof(fst));
tt = ;
for(int i=; i<=n; i++)p1[i]=i,p2[i]=i;
memset(h,-,sizeof(h));
}
void add(int u,int v,int w)
{
e[++tot].to = v;
e[tot].w = w;
e[tot].nxt = fst[u];
fst[u] = tot;
}
void add_Q(int u,int v,int idx)
{
Q[++tt].to = v;
Q[tt].nxt = h[u];
Q[tt].idx = idx;
h[u] = tt;
}
//kruscal
bool cmp(tree x,tree y)
{
return x.w<y.w;
}
int find_p(int x)
{
return x == p1[x] ? x : p1[x]=find_p(p1[x]);
}
void uone1(int x,int y)
{
int t1=find_p(x);
int t2=find_p(y);
if(t1!=t2)
{
if(rk1[t1]>rk1[t2])p1[t2]=t1;
else p1[t1]=t2;
if(rk1[t1]==rk1[t2])rk1[t2]++;
}
}
ll kruscal()
{
ll res = ;
sort(p+, p+m+, cmp);
int cnt=;
for (int i = ; i <= m; i++)
{
int x=p[i].a;
int y=p[i].b;
int w=p[i].w;
if(find_p(x)!=find_p(y))
{
cnt++;
uone1(x,y);
res+=w;
add(x,y,w);
add(y,x,w);
if(cnt==n-)break;
}
}
return res;
} //倍增
void init_fa(int u, int p, int w)
{
dep[u] = dep[p] + ;
fa[u][] = p;
max_e[u][] = w;
for (int i = ; fa[u][i-]; i++)
{
fa[u][i] = fa[ fa[u][i-] ][i-];
max_e[u][i] = max(max_e[u][i-], max_e[ fa[u][i-] ][i-]);
}
} int cal(int u, int lca)
{
int d = dep[u] - dep[lca];
int res = ;
for(int i = ; i >= ; i--)
{
if ((<<i) <= d)
{
d -= (<<i);
res = max(res, max_e[u][i]);
u = fa[u][i];
}
}
return res;
} //LCA
int find_q(int x)
{
return x == p2[x] ? x : p2[x]=find_q(p2[x]);
}
void uone2(int x,int y)
{
int t1=find_q(x);
int t2=find_q(y);
if(t1!=t2)
{
if(rk2[t1]>rk2[t2])p2[t2]=t1;
else p2[t1]=t2;
if(rk2[t1]==rk2[t2])rk2[t2]++;
}
}
void LCA(int u)
{
vis[u] = ;
acr[u] = u;
for(int p = fst[u]; p != -; p = e[p].nxt)
{
int v = e[p].to;
if(vis[v]) continue;
init_fa(v, u, e[p].w);
LCA(v);
uone2(u,v);
acr[find_q(u)] = u;
}
for(int p = h[u]; p != -; p = Q[p].nxt)
{
int v = Q[p].to;
if(vis[v]) ans[Q[p].idx] = acr[find_q(v)];
}
} int main()
{
// freopen("input.txt", "r", stdin);
scanf("%d%d", &n, &m);
CLS();
for (int i = ; i <= m; i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
p[i].a=s[i].a=a;
p[i].b=s[i].b=b;
p[i].w=s[i].w=w;
add_Q(a,b,i);
add_Q(b,a,i);
}
ll tmp = kruscal();
LCA();
for (int i = ; i <= m; i++)
printf("%I64d\n", tmp+s[i].w-max(cal(s[i].a, ans[i]), cal(s[i].b, ans[i])));
return ;
}