超时了
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 2e5+7;
struct node {
int x, y;
ll z;
int id;
bool operator < (const node &other) { return z < other.z; }
}a[N];
bool b[N];
int n, m;
int head[N*4], ver[N*4], nxt[N*4], tot;
void add(int x, int y) {
ver[++tot] = y;
nxt[tot] = head[x];
head[x] = tot;
}
inline int read() {
char c; int sign = 1;
while((c = getchar()) < '0' || c > '9') if(c == '-') sign = -1;
int res = c - '0';
while((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0';
return res * sign;
}
int dep[N], fa[N], top[N], sz[N], son[N];
void dfs1(int x,int fx) {
dep[x] = dep[fx] + 1;
fa[x] = fx;
sz[x] = 1;
for(int i = head[x]; i; i = nxt[i]) {
int y = ver[i];
if(y == fx) continue;
dfs1(y,x);
sz[x] += sz[y];
if(sz[son[x]] < sz[y]) son[x] = y;
}
}
void dfs2(int x,int TOP) {
top[x] = TOP;
if(!son[x]) return ;
dfs2(son[x],TOP);
for(int i = head[x]; i; i = nxt[i]) {
int y = ver[i];
if(y == fa[x] || y == son[x]) continue;
dfs2(y,y);
}
}
int lca(int x, int y) {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x,y);
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x,y);
return x;
}
int father[N*2], num;
ll val[N*2], ans[N];
int getfather(int x) {
if(father[x] == x) return father[x];
return father[x] = getfather(father[x]);
}
int main() {
n = read(), m = read();
for(int i = 1; i <= n*2; ++i) father[i] = i;
for(int i = 1; i <= m; ++i)
a[i].x = read(), a[i].y = read(), a[i].z = read(), a[i].id = i;
sort(a+1,a+1+m);
long long ANS = 0;
int num = n, LCA;
for(int i = 1; i <= m; ++i) {
int f1 = getfather(a[i].x);
int f2 = getfather(a[i].y);
if(f1 != f2) {
b[i] = 1;
father[f1] = father[f2] = ++num;
add(num, f1); add(num, f2); val; val[num] = a[i].z;
ANS += a[i].z;
}
}
dfs1(num,num); dfs2(num,num);
for(int i = 1; i <= m; ++i)
if(b[i]) ans[a[i].id] = ANS;
else LCA = lca(a[i].x,a[i].y), ans[a[i].id] = ANS-val[LCA]+ a[i].z ;
for(int i = 1; i <= m; ++i) printf("%lld\n",ans[i]);
return 0;
}