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:
/*  -*- coding: utf-8 -*-
 *
 * 2893.cc: Balanced Edge Deletion
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_GN = 100000;
const int MAX_M = 100000;
const long long LINF = 1LL << 60;

/* typedef */

typedef long long ll;
typedef queue<int> qi;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef stack<int> si;

struct Edge {
  int i, j, w;
  Edge() {}
  Edge(int _i, int _j, int _w): i(_i), j(_j), w(_w) {}
};

/* global variables */

vpii nbrs[MAX_N], gnbrs[MAX_GN];
Edge es[MAX_M];
int gmap[MAX_N], gps[MAX_GN], gpws[MAX_GN], gcns[MAX_GN];
ll gws[MAX_GN];

/* subroutines */

void bcc_visit(const vpii *nbrs, int v, int u,
	       vpii& brdg, vvi& tecomp,
	       si& roots, si& S, vb& inS, vi& num, int& time) {
  num[v] = ++time;
  S.push(v); inS[v] = true;
  roots.push(v);

  const vpii& nbrv = nbrs[v];
  for (vpii::const_iterator vit = nbrv.begin(); vit != nbrv.end(); vit++) {
    const int& w = vit->first;
    if (num[w] == 0)
      bcc_visit(nbrs, w, v, brdg, tecomp, roots, S, inS, num, time);
    else if (u != w && inS[w])
      while (num[roots.top()] > num[w]) roots.pop();
  }

  if (v == roots.top()) {
    brdg.push_back(pii(u, v));
    tecomp.push_back(vi());
    for (;;) {
      int w = S.top(); S.pop(); inS[w] = false;
      tecomp.back().push_back(w);
      if (v == w) break;
    }
    roots.pop();
  }
}

void calc_bcc(const int n, const vpii *nbrs, vpii& brdg, vvi& tecomp) {
  vi num(n);
  vb inS(n);
  si roots, S;
  int time = 0;

  for (int u = 0; u < n; u++)
    if (num[u] == 0) {
      bcc_visit(nbrs, u, n, brdg, tecomp, roots, S, inS, num, time);
      brdg.pop_back();
    }
}

inline ll absll(ll a) { return (a >= 0) ? a : -a; }

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);

  ll wsum = 0;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    u--, v--;
    nbrs[u].push_back(pii(v, i));
    nbrs[v].push_back(pii(u, i));
    es[i] = Edge(u, v, w);
    wsum += w;
  }

  vpii brdg;
  vvi tecomp;
  calc_bcc(n, nbrs, brdg, tecomp);
  //printf("brdg=%d\n", (int)brdg.size());

  int gn = tecomp.size();
  for (int i = 0; i < gn; i++)
    for (int j = 0; j < tecomp[i].size(); j++)
      gmap[tecomp[i][j]] = i;
  //for (int i = 0; i < n; i++) printf("%d ", gmap[i]); putchar('\n');

  for (int i = 0; i < m; i++) {
    Edge &ei = es[i];
    int &gi = gmap[ei.i], &gj = gmap[ei.j];

    if (gi == gj) gws[gi] += ei.w;
    else {
      gnbrs[gi].push_back(pii(gj, ei.w));
      gnbrs[gj].push_back(pii(gi, ei.w));
    }
  }

  gps[0] = -1, gpws[0] = 0;
  qi q;
  q.push(0);

  while (! q.empty()) {
    int u = q.front(); q.pop();
    int &up = gps[u];
    vpii &gnbru = gnbrs[u];
    for (vpii::iterator vit = gnbru.begin(); vit != gnbru.end(); vit++) {
      int &v = vit->first;
      if (v != up) {
	gps[v] = u, gpws[v] = vit->second;
	gcns[u]++;
	q.push(v);
      }
    }
  }

  for (int i = 0; i < gn; i++)
    if (gcns[i] == 0) q.push(i);

  while (! q.empty()) {
    int u = q.front(); q.pop();
    int &up = gps[u];
    if (up >= 0) {
      gws[up] += gws[u] + gpws[u];
      if (--gcns[up] == 0) q.push(up);
    }
  }

  ll mind = LINF;
  int mini = -1, minj = -1;

  for (int i = 0; i < m; i++) {
    Edge &ei = es[i];
    int &gi = gmap[ei.i], &gj = gmap[ei.j];

    ll d;
    if (gi == gj) 
      d = wsum - ei.w;
    else {
      if (gps[gi] == gj)
	d = absll((wsum - gws[gi] - gpws[gi]) - gws[gi]);
      else
	d = absll((wsum - gws[gj] - gpws[gj]) - gws[gj]);
    }

    if (mind > d ||
	(mind == d && (mini > ei.i || (mini == ei.j && minj > ei.j))))
      mind = d, mini = ei.i, minj = ei.j;
  }

  printf("%d %d\n", mini + 1, minj + 1);
  return 0;
}