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;
using Int = long long;
//INSERT ABOVE HERE
const Int INF = 1e17;
struct edge{
  Int w,u,v,to;
  edge(){}
  edge(Int w,Int u,Int v,Int to):w(w),u(u),v(v),to(to){}
  bool operator<(const edge &a)const{
    if(w!=a.w) return w<a.w;
    if(u!=a.u) return u<a.u;
    return v<a.v;
  }
};

struct LowLink{
  Int n,cnt;
  vector<vector<edge> > G;
  vector<Int> sum,used,pre,low,par;
  
  LowLink(Int n):n(n),cnt(0),G(n),sum(n),used(n),pre(n),low(n),par(n){}

  void add_edge(Int u,Int v,Int w){
    G[u].emplace_back(w,u,v,v);
    G[v].emplace_back(w,u,v,u);
  }

  void dfs(Int v,Int p){
    pre[v]=low[v]=cnt++;
    used[v]=1;
    for(auto e:G[v]){
      Int u=e.to;
      if(!used[u]){
	par[u]=v;
	dfs(u,v);
	low[v]=min(low[v],low[u]);
	sum[v]+=sum[u]+e.w;
      }else if(u!=p){
	low[v]=min(low[v],pre[u]);
        sum[u]+=e.w;
      }      
    }
  }
  
  edge build(){
    dfs(0,-1);
    edge ans(INF,0,0,0);
    Int all=sum[0];
    for(Int i=0;i<n;i++){
      for(edge e:G[i]){
	Int u=e.u,v=e.v;
	if(par[u]==v){	    
	  Int a=sum[u];
	  Int b=all-sum[u]-e.w;
	  edge e2(abs(a-b),u,v,-1);
	  if(pre[u]==low[u]&&e2<ans) ans=e2;
	}
	if(par[v]==u){
	  Int a=sum[v];
	  Int b=all-sum[v]-e.w;
	  edge e2(abs(a-b),u,v,-1);
	  if(pre[v]==low[v]&&e2<ans) ans=e2;
	}
	{
	  edge e2(all-e.w,u,v,-1);
	  if(e2<ans) ans=e2;	  
	}
      }
    }
    return ans;
  }
  
};



signed main(){
  Int n,m;
  cin>>n>>m;
  LowLink G(n);
  Int sum=0;
  for(Int i=0;i<m;i++){
    Int u,v,w;
    cin>>u>>v>>w;
    u--;v--;
    G.add_edge(u,v,w);
    sum+=w;
  }
  edge ans=G.build();
  //cout<<sum<<":"<<G.sum[0]<<endl;
  //cout<<ans.w<<endl;
  assert(sum==G.sum[0]);
  cout<<ans.u+1<<" "<<ans.v+1<<endl;
  return 0;
}