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;
#define int long long
#define REPF(i, a, n) for (int i = a; i < (n); i++)
#define REP(i, n) REPF(i, 0, n)
#define FORE(x, s) for (auto& x : s)
#define ALL(x) begin(x),end(x)
using pii = pair<int, int>;
template<class T> bool chmin(T&a, const T& b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
const int INF = 1001001001001001001LL;

int ans = INF;
pii ans_edge;
void update(int x, int a, int b) {
    if (ans > x) {
        ans = x;
        ans_edge = {min(a, b), max(a, b)};
    } else if (ans == x) {
        chmin(ans_edge, {min(a, b), max(a, b)});
    }
}
int total;
vector<vector<pii>> g;
vector<bool> used_v;
map<pii, bool> used_e;
vector<int> ord, lowlink, W;
int k = 0;
int dfs(int v) {
    used_v[v] = true;
    ord[v] = lowlink[v] = k++;
    for (auto& e : g[v]) {
        int w = e.first;
        int c = e.second;
        if (!used_v[w]) {
            used_e[{v, w}] = true;
            W[v] += dfs(w) + c;
            lowlink[v] = min(lowlink[v], lowlink[w]);  // lowlink[w]は計算済み
        } else if (!used_e[{w, v}]) {
            // v --> w は後退辺
            W[w] += c;
            lowlink[v] = min(lowlink[v], ord[w]);  // 後退辺を辿った先のordで更新(1度までしか後退辺を辿ることはできない)
        }
    }
    return W[v];
}

signed main() {
    ans_edge = {INF, INF};

    int N, M;
    cin >> N >> M;
    g = vector<vector<pii>>(N);
    used_v = vector<bool>(N, false);
    ord = lowlink = W = vector<int>(N, 0);

    REP(i, M) {
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        g[a].push_back({b, c});
        g[b].push_back({a, c});
        total += c;
    }
    dfs(0);

    vector<bool> vis(N, false);
    function<void(int)> dfs2 = [&](int v) {
        vis[v] = true;
        FORE(e, g[v]) {
            int w = e.first;
            int c = e.second;

            if (ord[v] < lowlink[w]) {
                int A = W[w];
                int B = total - A - c;
                update(abs(A - B), v, w);
            } else {
                update(total - c, v, w);
            }

            if (vis[w]) continue;
            dfs2(w);
        }
    };
    dfs2(0);

    cout << ans_edge.first + 1 << " " << ans_edge.second + 1 << endl;
    return 0;
}