Problem: E: Balanced Edge Deletion

problem

Given a weighted simple undirected graph G of N vertices and M edges. The vertices are numbered from 1 to N and the edges are numbered from 1 to M. The i-th edge connects the vertices u_i and v_i, and its cost is w_i.

Consider performing the following operation only once for this graph.

* Select one edge from the set of edges in G and delete that edge.



Since the graph may be divided by the above operation, the graph after the operation will be referred to as A and B. (If not split, B is an empty graph.) Let W (X) be the sum of the costs of the edges in graph X (W (X) = 0 if there are no edges in the graph) and $ \ mathrm Define {cost} $ (A, B) = | W (A) −W (B) |. Find the edge (u, v) that minimizes $ \ mathrm {cost} $ (A, B). If there are more than one, answer the one with the smallest u. If there are still more than one, answer the one with the smallest v.

Input format

The input is given in the following format:


N M
u_1 v_1 w_1
...
u_M v_M w_M


Constraint

* 2 \ leq N \ leq 10 ^ 5
* 1 \ leq M \ leq 10 ^ 5
* 1 \ leq u_i <v_i \ leq N
* If i \ neq j, u_i \ neq u_j or v_i \ neq v_j
* 1 \ leq w_i \ leq 10 ^ 9



The graph given is concatenated.

Output format

Output the answer side to one line separated by blanks as shown below.


u v

* u, v are integers
* 1 \ leq u, v \ leq N



Input example 1


5 4
1 2 1
2 3 10
3 4 5
4 5 1


Output example 1


twenty three

Input example 2


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


Output example 2


5 6





Example

Input

5 4
1 2 1
2 3 10
3 4 5
4 5 1


Output

2 3

Solution (in CPP):

Solution:
 #include <bits/stdc++.h>
using namespace std;

#define int long long
const int INF = 1e16;

struct BC {
  typedef pair<int, int> P;
  int n;
  vector<vector<int> > G, C, T;
  vector<int> ord, low, bel;
  vector<P> B;
  BC() {}
  BC(int sz) : n(sz), G(sz), C(sz), T(sz) {}

  void add_edge(int u, int v) {
    G[u].push_back(v);
    G[v].push_back(u);
  }

  bool is_bridge(int u, int v) {
    if ( ord[u] > ord[v] ) swap(u, v);
    return ord[u] < low[v];    
  }

  void dfs(int v, int p, int &k) {
    ord[v] = low[v] = k;
    k++;
    for ( int u : G[v] ) {
      if ( u == p ) continue;
      if ( ord[u] >= 0 ) {
	low[v] = min(low[v], ord[u]);
      } else {
	dfs(u, v, k);
	low[v] = min(low[v], low[u]);
      }
      if ( is_bridge(u, v) ) B.push_back(P(u, v));      
    }
  }

  void fill_component(int c, int v) {
    C[c].push_back(v);
    bel[v] = c;
    for ( int u : G[v] ) {
      if ( bel[u] >= 0 || is_bridge(u, v) ) continue;
      fill_component(c, u);      
    }
  }

  void add_component(int v, int &k) {
    if ( bel[v] >= 0 ) return;
    fill_component(k++, v);    
  }

  int build() {
    int k = 0;
    ord.resize(n);
    low.resize(n);
    bel.resize(n);
    fill(ord.begin(), ord.end(), -1);
    fill(bel.begin(), bel.end(), -1);
    for ( int v = 0; v < n; v++ ) {
      if ( ord[v] >= 0 ) continue;
      dfs(v, -1, k);
    }
    k = 0;
    for ( int i = 0; i < (int)B.size(); i++ ) {
      add_component(B[i].first, k);
      add_component(B[i].second, k);
    }
    for ( int v = 0; v < n; v++ ) add_component(v, k);
    for ( int i = 0; i < (int)B.size(); i++ ) {
      int u = bel[B[i].first], v = bel[B[i].second];
      T[u].push_back(v);
      T[v].push_back(u);
    }
    return k;    
  }
};

struct Edge {
  int u, v, w;
};

int N, M;
vector<Edge> G;
vector<int> sum;
vector<int> sub;
vector<vector<int> > T;
int t;
vector<int> bel;
map<int, int> cost[100001];

int calc_sum(int u, int p) {
  int ret = sum[u];
  for ( int v : T[u] ) {
    if ( v == p ) continue;   
    ret += cost[u][v];
    ret += calc_sum(v, u);    
  }

  return sub[u] = ret;  
}

signed main() {
  cin >> N >> M;
  pair<int, int> ans = {INF, INF};

  BC bc(N);
  for ( int i = 0; i < M; i++ ) {
    int u, v, w;
    cin >> u >> v >> w;
    u--; v--;
    bc.add_edge(u, v);
    G.push_back(Edge{u, v, w});    
  }

  t = bc.build();
  T = bc.T;
  bel = bc.bel;  
  
  sum = sub = vector<int>(N, 0);

  for ( int i = 0; i < M; i++ ) {
    int u = G[i].u, v = G[i].v, w = G[i].w;    
    if ( bel[u] == bel[v] ) {
      sum[bel[u]] += w;
    } else {
      cost[bel[u]][bel[v]] += w;
      cost[bel[v]][bel[u]] += w;      
    }
  }

  calc_sum(0, -1);

  int mi = INF;  
  int all = sub[0];
  for ( int i = 0; i < M; i++ ) {
    int u = G[i].u, v = G[i].v, w = G[i].w;
    if ( mi > all-w ) {
      mi = all-w;
      ans = {u, v};      
    } else if ( mi == all-w ) {
      ans = min(ans, {u, v});      
    }
    if ( bel[u] == bel[v] ) continue;
    int ab = abs((all-(min(sub[bel[u]], sub[bel[v]]))-w) - min(sub[bel[u]], sub[bel[v]]));    
    if ( mi > ab ) {
      mi = ab;      
      ans = {u, v};      
    } else if ( mi == ab ) {
      ans = min(ans, {u, v});
    }
  }

  cout << ans.first+1 << " " << ans.second+1 << endl;  
  
  return 0;
}