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;

typedef long long ll;
typedef pair<ll,ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define minch(x,y) x=min(x,y)
#define dbg(x) cerr<<#x<<"="<<x<<endl

#define INF 5e18

struct UnionFind{
  vector<int> v;
  UnionFind(int n) : v(n,-1){}
  int find(int x){ return v[x] < 0 ? x : v[x]=find(v[x]); }
  bool unite(int x,int y){
    x=find(x); y=find(y);
    if(x==y)return false;
    if(-v[x] < -v[y])swap(x,y);
    v[x]+=v[y]; v[y]=x;
    return true;
  }
};

int N,M;
vector<int> g[100010];
vector<P> t[100010];
ll vw[100010];
map<P,ll> w;
ll ord[100010],low[100010];
ll cnt;
vector<P> es;
map<P,bool> isbridge;
ll res=INF,tot=0;
P resv;
map<P,P> redge;

void dfs(int v,int prev){
  ord[v]=low[v]=++cnt;
  for(int nv : g[v]){
    if(ord[nv]==-1){
      dfs(nv,v);
      low[v]=min(low[v],low[nv]);
      if(ord[v]<low[nv]){
        isbridge[P(min(v,nv),max(v,nv))]=true;
      }
    }else if(nv!=prev){
      low[v]=min(low[v],ord[nv]);
    }
  }
}

ll dfs2(int v,int pre){
  ll sum=vw[v];
  for(P e : t[v]){
    if(e.fi==pre)continue;
    sum+=dfs2(e.fi,v)+e.se;
  }
  for(P e : t[v]){
    if(e.fi==pre){
      tot-=e.se;
      if(res>=llabs(tot-sum-sum)){
        if(res==llabs(tot-sum-sum))minch(resv,redge[P(v,e.fi)]);
        else resv=redge[P(v,e.fi)];
        res=llabs(tot-sum-sum);
      }
      tot+=e.se;
      break;
    }
  }
  return sum;
}

int main(){
  cin>>N>>M;
  rep(i,M){
    ll a,b,c;
    cin>>a>>b>>c;
    a--;b--;
    g[a].push_back(b);
    g[b].push_back(a);
    w[P(a,b)]=c;
    tot+=c;
    es.push_back(P(a,b));
  }
  rep(i,M){
    if(res>=tot-w[es[i]]){
      if(res==tot-w[es[i]])minch(resv,es[i]);
      else resv=es[i];
      res=tot-w[es[i]];
    }
  }
  memset(ord,-1,sizeof(ord));
  cnt=0;
  dfs(0,-1);

  UnionFind uf(N);
  rep(i,M){
    if(isbridge[es[i]])continue;
    uf.unite(es[i].fi,es[i].se);
  }
  rep(i,M){
    int u=uf.find(es[i].fi);
    int v=uf.find(es[i].se);
    if(u!=v){
      t[u].push_back(P(v,w[es[i]]));
      t[v].push_back(P(u,w[es[i]]));
      redge[P(u,v)]=redge[P(v,u)]=es[i];
    }else{
      vw[u]+=w[es[i]];
    }
  }

  dfs2(uf.find(0),-1);
  cout<<resv.fi+1<<" "<<resv.se+1<<endl;

  return 0;
}