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
using int64 = long long;

int N, M;
vector< int > g[100000];
bool used[100000];
int low[100000], ord[100000], comp[100000];
vector< pair< int, int > > bridge;

int build(int idx, int k = 0, int par = -1) {
  used[idx] = true;
  ord[idx] = k++;
  low[idx] = ord[idx];
  int cnt = 0;
  for(auto &to : g[idx]) {
    if(!used[to]) {
      ++cnt;
      k = build(to, k, idx);
      low[idx] = min(low[idx], low[to]);
      if(ord[idx] < low[to]) bridge.emplace_back(minmax(idx, to));
    } else if(to != par) {
      low[idx] = min(low[idx], ord[to]);
    }
  }
  return k;
}


using int64 = long long;


vector< vector< pair< int, pair< int, int > > > > uku;
map< pair< int, int >, int > cost;


vector< vector< pair< int, pair< int, int > > > > latte() {
  build(0);
  int k = 0;
  function< void(int, int) > dfs = [&](int idx, int par) {
    if(~par && ord[par] >= low[idx]) comp[idx] = comp[par];
    else comp[idx] = k++;
    for(auto &to : g[idx]) {
      if(comp[to] == -1) dfs(to, idx);
    }
  };
  memset(comp, -1, sizeof(comp));
  dfs(0, -1);
  vector< vector< pair< int, pair< int, int > > > > t(k);
  int ptr = 0;
  for(auto &e : bridge) {
    int x = comp[e.first], y = comp[e.second];
    int c = cost[minmax(e.first, e.second)];
    if(x != y) {
      t[x].emplace_back(y, make_pair(c, ptr));
      t[y].emplace_back(x, make_pair(c, ptr));
      ++ptr;
    }
  }
  return t;
}

int64 sum = 0;
int64 weight[100000];
pair< int64, pair< int, int > > ret(1LL << 60, make_pair(1333, 1333));

void usa(int idx, int par) {
  for(auto &e : uku[idx]) {
    if(e.first == par) continue;
    usa(e.first, idx);
    weight[idx] += weight[e.first];
    weight[idx] += e.second.first;
  }
  for(auto &e : uku[idx]) {
    if(e.first == par) continue;
    int64 Latte = weight[e.first];
    int64 Malta = sum - Latte - e.second.first;
    ret = min(ret, make_pair(llabs(Latte - Malta), bridge[e.second.second]));
  }
}

signed main() {
  cin >> N >> M;
  for(int i = 0; i < M; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    --u, --v;
    g[u].emplace_back(v);
    g[v].emplace_back(u);
    cost[minmax(u, v)] = w;
    sum += w;
  }
  uku = latte();

  for(int i = 0; i < N; i++) {
    for(auto &j : g[i]) {
      if(i < j) {
        if(comp[i] == comp[j]) {
          weight[comp[i]] += cost[{i, j}];
        }
        ret = min(ret, make_pair(sum - cost[{i, j}], make_pair(i, j)));
      }
    }
  }

  usa(0, -1);

  cout << ret.second.first + 1 << " " << ret.second.second + 1 << endl;


}