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<int,int> pii;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)

int n,m;
vector<pii> g[125252];

// スパソを写経しました
vector<pii> bridges;
vector<vector<int>> islands;
int num[125252];
bool inS[125252];
stack<int> roots, S;
int tim = 0;
void visit(int v,int u){
  num[v] = ++tim;
  S.push(v);inS[v]=true;
  roots.push(v);
  for(pii P : g[v]){
    int to = P.first;
    if(num[to]==0){
      visit(to,v);
    }else if(u!=to && inS[to]){
      while(num[roots.top()] > num[to])roots.pop();
    }
  }
  if(v == roots.top()){
    bridges.push_back(pii(u,v));
    islands.push_back(vector<int>());
    while(1){
      int w = S.top(); S.pop(); inS[w] = false;
      islands.back().push_back(w);
      if(v==w)break;
    }
    roots.pop();
  }
}
void bridge(){
  REP(u,n)if(num[u]==0){
    visit(u,n);
    bridges.pop_back();
  }
}

int mp[125252];
ll wsum[125252];
ll wall;
vector<pii> g2[125252];

ll mini = 1e18;
set<pii> SS;

ll csum[125252];

void dfs(int p, int bef){
  csum[p] += wsum[p];
  for(pii P : g2[p]){
    int to = P.first;
    ll w = P.second;
    if(to == bef)continue;
    dfs(to, p);
    csum[p] += csum[to] + w;
    ll cost = abs(csum[to] - (wall - csum[to] - w));
    if(cost < mini){
      mini = cost;
      SS.clear();
    }
    if(cost == mini){
      SS.insert(pii(min(p,to),max(p,to)));
    }
  }
}

int n2 = islands.size();

int main(){
  scanf("%d%d",&n,&m);
  wall = 0;
  REP(i,m){
    int u,v,w;
    scanf("%d%d%d",&u,&v,&w);
    --u;--v;
    g[u].push_back(pii(v,w));
    g[v].push_back(pii(u,w));
    wall += w;
  }
  bridge();
  // oh じゃあないんだよ
  // if(islands.size()==1){
  //   // oh
  //   int ansu = n+1, ansv = n+1;
  //   REP(i,n)for(pii P : g[i]){
  //     int j = P.first;
  //     if(i>j)continue;
  //     if(i<ansu || (i==ansu && j<ansv)){
  //       ansu = i;
  //       ansv = j;
  //     }
  //   }
  //   printf("%d %d\n",ansu+1,ansv+1);
  //   return 0;
  // }
  REP(i,islands.size()){
    for(int x : islands[i]){
      mp[x] = i;
    }
  }
  REP(i,n)for(pii P : g[i]){
    int to = P.first;
    int w = P.second;
    if(i>to)continue;
    if(mp[i]==mp[to]){
      wsum[mp[i]] += w;
    }else{
      g2[mp[i]].push_back(pii(mp[to], w));
      g2[mp[to]].push_back(pii(mp[i], w));
    }
  }
  dfs(0,-1);
  int ansu = n+1, ansv = n+1;
  REP(i,n)for(pii P : g[i]){
    int to = P.first;
    int w = P.second;
    if(i>to)continue;
    if(mp[i]==mp[to]){
      continue;
    }else{
      pii Q = pii(min(mp[i],mp[to]),max(mp[i],mp[to]));
      if(SS.count(Q)){
        if(i<ansu || (i==ansu && to<ansv)){
          ansu = i;
          ansv = to;
        }
      }
    }
  }
  REP(i,n)for(pii P : g[i]){
    int to = P.first;
    int w = P.second;
    if(i>to)continue;
    ll cost = wall-w;
    if(cost < mini){
      mini = cost;
      ansu = i;
      ansv = to;
    }else if(cost == mini){
      if(i<ansu || (i==ansu && to<ansv)){
        ansu = i;
        ansv = to;
      }
    }
  }
  printf("%d %d\n",ansu+1,ansv+1);
  return 0;
}