Problem:
E - Minimum Spanning Tree

Problem Statement

You are given an undirected weighted graph G with n nodes and m edges.
Each edge is numbered from 1 to m.

Let G_i be an graph that is made by erasing i-th edge from G. Your task is to compute the cost of minimum spanning tree in G_i for each i.

Input

The dataset is formatted as follows.

n m
a_1 b_1 w_1
...
a_m b_m w_m

The first line of the input contains two integers n (2 \leq n \leq 100{,}000) and m (1 \leq m \leq 200{,}000).
n is the number of nodes and m is the number of edges in the graph.
Then m lines follow, each of which contains a_i (1 \leq a_i \leq n), b_i (1 \leq b_i \leq n) and w_i (0 \leq w_i \leq 1{,}000{,}000).
This means that there is an edge between node a_i and node b_i and its cost is w_i.
It is guaranteed that the given graph is simple: That is, for any pair of nodes, there is at most one edge that connects them, and a_i \neq b_i for all i.

Output

Print the cost of minimum spanning tree in G_i for each i, in m line. If there is no spanning trees in G_i, print "-1" (quotes for clarity) instead.

Sample Input 1

4 6
1 2 2
1 3 6
1 4 3
2 3 1
2 4 4
3 4 5

Output for the Sample Input 1

8
6
7
10
6
6

Sample Input 2

4 4
1 2 1
1 3 10
2 3 100
3 4 1000

Output for the Sample Input 2

1110
1101
1011
-1

Sample Input 3

7 10
1 2 1
1 3 2
2 3 3
2 4 4
2 5 5
3 6 6
3 7 7
4 5 8
5 6 9
6 7 10

Output for the Sample Input 3

27
26
25
29
28
28
28
25
25
25

Sample Input 4

3 1
1 3 999

Output for the Sample Input 4

-1

Solution in C++14 (judge verdict: Time Limit Exceeded)

#include <bits/stdc++.h>
#define N 100011
#define MAX_V 100010
#define MAX_LOG_V 20
#define INF (1e9)
using namespace std;
vector<int> G[MAX_V];
int root;
int parent[MAX_LOG_V][MAX_V];
int depth[MAX_V];
  
void dfs(int v,int p,int d){
  parent[0][v]=p;
  depth[v]=d;
  for(int i=0;i<G[v].size();i++) if(G[v][i] != p)dfs(G[v][i],v,d+1);
}
  
void init(int V){
  dfs(root,-1,0);
  for(int k=0; k+1<MAX_LOG_V ;k++)
    for(int v=0; v<V ;v++)
      if(parent[k][v] < 0)parent[k+1][v] = -1;
      else parent[k+1][v] = parent[k][parent[k][v]];
}
  
int lca(int u,int v){
  if(depth[u]>depth[v]) swap(u,v);
  for(int k=0; k<MAX_LOG_V ;k++)
    if((depth[v]-depth[u])>>k&1)v=parent[k][v];
  if(u==v) return u;
  
  for(int k=MAX_LOG_V-1; k>=0 ;k--)
    if(parent[k][u] != parent[k][v]){
      u=parent[k][u];
      v=parent[k][v];
    }
  return parent[0][u];
}
  
typedef long long ll;
struct edge{int to,cost,idx;};
typedef pair<int,int> P;
typedef pair<ll,P> PP;
vector<edge> g[N];
int MST[N]={};
map<P,int> M;
int n,m;
  
void add_edge(int a,int b,int cost,int idx){
  g[a].push_back((edge){b,cost,idx});
  g[b].push_back((edge){a,cost,idx});
}
  
ll prim(){
  memset(MST,-1,sizeof(MST));
  ll res=0;
  priority_queue<PP,vector<PP>,greater<PP> >Q;
  Q.push(PP(0,P(0,0)));
  while(!Q.empty()){
    PP t = Q.top();Q.pop();
    ll cost=t.first;
    int pos=t.second.first,pre=t.second.second;
    if(MST[pos]!=-1)continue;
    MST[pos] = pre;
    res+=cost;   
    for(int i=0;i<g[pos].size();i++){
      int to=g[pos][i].to;
      int cost=g[pos][i].cost;
      if(MST[to]!=-1)continue;
      Q.push(PP(cost,P(to,pos)));
    }
  }
  return res;
}
  
  
typedef priority_queue<P,vector<P>,greater<P> > PQ;
int ans[200001],dep=0,pos[N],pre[N];
 
bool used[N]={};  
void dfs(PQ &res){
  res.push(P(INF,-1));
  for(int i=0;i<g[pos[dep]].size();i++){
    int LCA=lca(pos[dep],g[pos[dep]][i].to);
    if(g[pos[dep]][i].to==pre[dep]||LCA==pos[dep])continue;
    res.push(P(g[pos[dep]][i].cost,LCA));
  }
  
 
  for(int i=0;i<G[pos[dep]].size();i++){
    dep++;
    pos[dep] = G[pos[dep-1]][i];
    pre[dep] = pos[dep-1];
    PQ t;
    dfs(t);
    dep--;
    while(!t.empty()){
      if(t.top().second!=pos[dep])res.push(t.top());
      t.pop();
    }
  }
 
  PQ t;
  memset(used,0,sizeof(used));
  while(!res.empty()){
    if(!used[res.top().second]) t.push(res.top()),used[res.top().second] = 1;
    res.pop();
  }
  res=t;
  if(pos[dep])ans[M[P(pos[dep],pre[dep])]] = res.top().first;
}
  
int main(){
  cin>>n>>m;
  vector<PP> e(m);
  for(int i=0,a,b,c;i<m;i++){
    scanf("%d%d%d",&a,&b,&c);
    a--,b--;
    e[i]=PP(c,P(a,b));
    M[P(a,b)]=M[P(b,a)]=i;
    add_edge(a,b,c,i);
  }
  
  ll mncost=prim();
  int used[200011]={},flg=0;
  for(int i=1;i<n;i++){
    flg|=MST[i]==-1;
    if(MST[i]==-1)continue;
    G[MST[i]].push_back(i);
    used[M[P(i,MST[i])]]=1;
  }
  init(n);
  pos[0]=0,pre[0]=-1;
  PQ t;
  dfs(t);
  for(int i=0;i<m;i++){
    if(!used[i]&&!flg) printf("%lld\n",mncost);
    else if(ans[i]>=INF)printf("-1\n");
    else printf("%lld\n",mncost-e[i].first+ans[i]);
  }
  return 0;
}