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 <algorithm>
#include <vector>
#include <iostream>
#include <cstdio>

using namespace std;

typedef long long ll;

#define SIZE 100010

int u[SIZE],v[SIZE],w[SIZE];
ll wmemo[SIZE];

pair<int,ll> dfs(int now, int path, vector<vector<pair<int,int> > > &way,
        vector<int> &visited, vector<int> &sum, vector<bool> &is_bridge){
  int res_sum = 0;
  visited[now] = 1;

  ll sumw = 0;
  
  for(int i=0;i<way[now].size();i++){
    auto p = way[now][i];
    if(p.second == path) continue;

    if(visited[p.first] == 1){
      sum[p.first]--;
      sum[now]++;
      is_bridge[p.second] = false;
      sumw += w[p.second];
    }else if(visited[p.first] == 0){
      auto res = dfs(p.first, p.second, way, visited, sum, is_bridge);
      res_sum += res.first;
      sumw += res.second + w[p.second]; 
      if(res.first > 0) is_bridge[p.second] = false;
    }
  }

  visited[now] = 2;
  wmemo[path] = sumw;
  return {res_sum + sum[now], sumw};  
}


int main(){
  int n, m, maxw_i;
  ll sumw = 0, maxw = 0;
  
  scanf("%d%d", &n, &m);

  vector<vector<pair<int,int> > > way(n);
  vector<int> visited(n, 0), sum(n, 0);
  vector<bool> is_bridge(m, true);
  
  for(int i=0;i<m;i++){
    scanf("%d%d%d", u+i, v+i, w+i);
    sumw += w[i];
    if(maxw < w[i] || (maxw == w[i] && (u[maxw_i] > u[i] || (u[maxw_i] == u[i] && v[maxw_i] > v[i])))){
      maxw = w[i]; maxw_i = i;
    }
    u[i]--; v[i]--;
    
    way[u[i]].push_back({v[i], i});
    way[v[i]].push_back({u[i], i});
  }

  dfs(0, -1, way, visited, sum, is_bridge);
  
  ll ans1 = sumw - maxw;
  ll ans2 = maxw_i;
  
  for(int i=0;i<m;i++){
    if(is_bridge[i]){
      ll calc = abs((sumw - wmemo[i] - w[i]) - wmemo[i]);
      if(ans1 >= calc){
        if(ans1 > calc || (u[ans2] > u[i] || (u[ans2] == u[i] && v[ans2] > v[i])))
        ans1 = calc;
        ans2 = i;
      }
    }
  }

  //debug(ans1);
  
  printf("%d %d\n", u[ans2]+1, v[ans2]+1);
}