Language: C++

#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e5+7;
vector<int> V[maxn], W[maxn], ind[maxn];
int n, m, x, y, z, f[maxn], head[maxn], sonson[maxn], dep[maxn], Q[maxn], size[maxn], belong[maxn], cnt, id[maxn],
fz[maxn<<1];
int find( int x ){
    return ( x == f[x] ) ? f[x] : ( f[x] = find( f[x] ) );
}
struct Edge{
    int u, v, w, id;
    bool vis;
    void in( int ID ){
        scanf( "%d %d %d" , &u , &v , &w );
        id = ID;
        vis = false;
    }
}edge[maxn<<1];
bool operator < ( const Edge &a , const Edge &b ){
    return a.w < b.w;
}
bool cmp( const Edge &a , const Edge &b ){
    return a.id < b.id;
}
void split(){
    int l = 0, r = 1;
    dep[Q[r]=1]=1;
    f[1] = -1;
    while( l < r ){
        int x = Q[++l];
        if( V[x].empty() )
            continue;
        for( int j = V[x].size() - 1 ; j >= 0 ; j -- ){
            int y = V[x][j];
            if( y == f[x] )
                continue;
            dep[Q[++r]=y] = dep[x] + 1;
            fz[W[x][j]] = y;
            f[y] = x;
        }
    }
    for( int i = n ; i ; i -- ){
        int x = Q[i], p = -1;
        size[x] = 1;
        if( V[x].empty() )
            continue;
        for( int j = V[x].size() - 1 ; j >= 0 ; j -- ){
            int y = V[x][j];
            if( y == f[x] )
                continue;
            size[x] += size[y];
            if( p == -1 || ( p > 0 && size[y] > size[p] ) )
                p = y;
        }
        if( p == -1 ){
            sonson[++cnt] = x;
            belong[head[cnt]=x] = cnt;
        }else{
            belong[x] = belong[p];
            head[belong[x]] = x;
        }
    }
}
void doit( int a , int b , int hehe ){
    int ida = belong[a], idb = belong[b], ha = head[iad],
    hb = head[idb];
    while( ida != idb ){
        if( dep[ha] > dep[hb] ){
            modify( id[a] , id[ha] , hehe );
            a = f[ha];
        }else{
            modify( id[a] , id[hb] , hehe );
            b = f[hb];
        }
        ida = belong[a], idb = belong[b], ha= head[ida], hb = head[idb];
    }

}
int main(){
    scanf( "%d %d" , &n , &m );
    for( int i = 1 ; i <= m ; i ++ )
        edge[i].in( i );
    for( int i = 1 ; i <= n ; i ++ )
        f[i] = i;
    sort( edge + 1 , edge + m + 1 );
    long long ans = 0;
    for( int i = 1 ; i <= m ; i ++ ){
        int u = find( edge[i].u ), v = find( edge[i].v );
        if( u == v )
            continue;
        f[u] = f[v];
        ans = ans + edge[i].w;
        u = edge[i].u, v = edge[i].v;
        V[u].push_back(v);
        V[v].push_back(u);
        W[u].push_back( edge[i].id );
        W[v].push_back( edge[i].id );
        edge[i].vis = 1;
    }
    split();
    int count = 0;
//    build( 1 , 1 , n );
    for( int i = 1 ; i <= cnt ; i ++ ){
        int y = sonson[i];
        while( true ){
            id[y] = ++ count;
            //
            if( f[y] == -1 )
                break;
            if( belong[y] != belong[f[y]] )
                break;
            y = f[y];
        }
    }
    sort( edge + 1 , edge + m + 1 , cmp );
    for( int i = 1 ; i <= m ; i ++ ){
        int u = edge[i].u, v = edge[i].v;
        doit( u , v );
    }
    for( int i = 1 ; i <= m ; i ++ ){
        if( !edge[i].vis ){
            printf( "%lld\n" , ans );
            continue;
        }
        printf( "%lld\n" , ans - edge[i].w ); //+ query() );
    }
	return 0;
}

Compiler output:
a.cc: In function 'void split()':
a.cc:52:9: error: reference to 'size' is ambiguous
   52 |         size[x] = 1;
      |         ^~~~
In file included from /usr/include/c++/14/map:65,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:13:75: note:                 'int size [100007]'
   13 | int n, m, x, y, z, f[maxn], head[maxn], sonson[maxn], dep[maxn], Q[maxn], size[maxn], belong[maxn], cnt, id[maxn],
      |                                                                           ^~~~
a.cc:59:13: error: reference to 'size' is ambiguous
   59 |             size[x] += size[y];
      |             ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:13:75: note:                 'int size [100007]'
   13 | int n, m, x, y, z, f[maxn], head[maxn], sonson[maxn], dep[maxn], Q[maxn], size[maxn], belong[maxn], cnt, id[maxn],
      |                                                                           ^~~~
a.cc:59:24: error: reference to 'size' is ambiguous
   59 |             size[x] += size[y];
      |                        ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:13:75: note:                 'int size [100007]'
   13 | int n, m, x, y, z, f[maxn], head[maxn], sonson[maxn], dep[maxn], Q[maxn], size[maxn], belong[maxn], cnt, id[maxn],
      |                                                                           ^~~~
a.cc:60:39: error: reference to 'size' is ambiguous
   60 |             if( p == -1 || ( p > 0 && size[y] > size[p] ) )
      |                                       ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:13:75: note:                 'int size [100007]'
   13 | int n, m, x, y, z, f[maxn], head[maxn], sonson[maxn], dep[maxn], Q[maxn], size[maxn], belong[maxn], cnt, id[maxn],
      |                                                                           ^~~~
a.cc:60:49: error: reference to 'size' is ambiguous
   60 |             if( p == -1 || ( p > 0 && size[y] > size[p] ) )
      |                                                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:13:75: note:                 'int size [100007]'
   13 | int n, m, x, y, z, f[maxn], head[maxn], sonson[maxn], dep[maxn], Q[maxn], size[maxn], belong[maxn], cnt, id[maxn],
      |                                                                           ^~~~
a.cc: In function 'void doit(int, int, int)':
a.cc:73:53: error: 'iad' was not declared in this scope; did you mean 'ida'?
   73 |     int ida = belong[a], idb = belong[b], ha = head[iad],
      |                                                     ^~~
      |                                                     ida
a.cc:76:27: error: 'hb' was not declared in this scope; did you mean 'ha'?
   76 |         if( dep[ha] > dep[hb] ){
      |                           ^~
      |                           ha
a.cc:77:13: error: 'modify' was not declared in this scope; did you mean 'modfl'?
   77 |             modify( id[a] , id[ha] , hehe );
      |             ^~~~~~
      |             modfl
a.cc:80:13: error: 'modify' was not declared in this scope; did you mean 'modfl'?
   80 |             modify( id[a] , id[hb] , hehe );
      |             ^~~~~~
      |             modfl
a.cc:83:58: error: 'hb' was not declared in this scope; did you mean 'ha'?
   83 |         ida = belong[a], idb = belong[b], ha= head[ida], hb = head[idb];
      |                                                          ^~
      |                                                          ha
a.cc: In function 'int main()':
a.cc:126:13: error: too few arguments to function 'void doit(int, int, int)'
  126 |         doit( u , v );
      |         ~~~~^~~~~~~~~
a.cc:72:6: note: declared here
   72 | void doit( int a , int b , int hehe ){
      |      ^~~~