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 REP(i,n) for (int i=0;i<(n);i++)
#define REP2(i,m,n) for (int i=m;i<(n);i++)
typedef long long ll;
typedef long double ld;
#define fst first
#define snd second
#define all(c) ((c).begin()), ((c).end())

struct Graph {
    int n;
    vector<vector<int>> adj;
    Graph(int n) : n(n), adj(n) { }
    void addEdge(int src, int dst) {
        adj[src].push_back(dst);
        adj[dst].push_back(src);
    }
};

struct BridgeBlockTree : Graph {
    vector<int> index;         // index[u] is the block index containing u
    vector<vector<int>> block; // u in block[k] <=> index[u] == k

    BridgeBlockTree(Graph g) : Graph(0) {
        index.assign(g.n, -1);
        vector<int> num(g.n), par(g.n,-1), cur(g.n);

        for (int s = 0; s < g.n; ++s) {
            if (num[s]) continue;
            int time = 0;
            vector<int> snum, path, stack = {s};
            while (!stack.empty()) {
                int u = stack.back();
                if (cur[u] == 0) {
                    num[u] = ++time;
                    path.push_back(u);
                    snum.push_back(num[u]);
                }
                if (cur[u] == g.adj[u].size()) {
                    if (num[u] == snum.back()) {
                        snum.pop_back();
                        block.push_back({});
                        while (1) {
                            int w = path.back(); path.pop_back();
                            block.back().push_back(w);
                            index[w] = block.size()-1;
                            if (u == w) break;
                        }
                    }
                    stack.pop_back();
                } else {
                    int v = g.adj[u][cur[u]++];
                    if (!num[v]) {
                        par[v] = u;
                        stack.push_back(v);
                    } else if (v != par[u] && index[v] < 0) {
                        while (snum.back() > num[v]) snum.pop_back();
                    }
                }
            }
        }
        n = block.size();
        adj.resize(n);
        for (int u = 0; u < g.n; ++u)
            if (par[u] >= 0 && index[u] != index[par[u]])
                addEdge(index[u], index[par[u]]);
    }
};

int N, M, u, v;
ll w;
map<int, ll> W[101010];
map<int, map<int, pair<int,int>>> mp;
ll WS[101010];
ll all_sum = 0;
ll ans = 1LL << 59;
int ans_u;
int ans_v;


ll dfs(int n, int p, BridgeBlockTree &t) {
    ll tmp = WS[n];
    for (auto m: t.adj[n]) {
        if (m != p)
            tmp += dfs(m, n, t);
    }
    if (p == -1) {
        return 0LL;
    }

    ll wp = W[mp[n][p].first][mp[n][p].second];
    ll tmp2 = abs((all_sum - tmp - wp) - tmp);

    if (tmp2 == ans) {
        if (mp[n][p].first < ans_u) {
            ans_u = mp[n][p].first;
            ans_v = mp[n][p].second;
        } else if (mp[n][p].first == ans_u && mp[n][p].second < ans_v) {
            ans_v = mp[n][p].second;
        }
    } else if (tmp2 < ans) {
        ans = tmp2;
        ans_u = mp[n][p].first;
        ans_v = mp[n][p].second;
    }

    return tmp + wp;
}


void solve() {
    cin >> N >> M;
    Graph g(N);

    REP(i, M) {
        cin >> u >> v >> w;
        u--; v--;
        g.addEdge(u, v);
        W[u][v] = w;
        W[v][u] = w;
        all_sum += w;
    }
    BridgeBlockTree t(g);

    for (int i = 0; i < N; ++i)
        for (auto j: g.adj[i])
            if (i < j && t.index[i] == t.index[j]) {
                WS[t.index[i]] += W[i][j];
                ll tmp = all_sum - W[i][j];
                if (tmp < ans) {
                    ans = tmp;
                    ans_u = i;
                    ans_v = j;
                } else if (tmp == ans) {
                    if (i < ans_u) {
                        ans_u = i;
                        ans_v = j;
                    } else if (i == ans_u && j < ans_v) {
                        ans_v = j;
                    }
                }
            }
            else if (i < j) {
                mp[t.index[i]][t.index[j]] = make_pair(i, j);
                mp[t.index[j]][t.index[i]] = make_pair(i, j);
            }

    dfs(0, -1, t);
    cout << ans_u + 1 << " " << ans_v + 1 << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
}