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<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<double,ll> pdl;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;

//無向
class Bridge{
private:
    typedef vector<vector<ll>> edge;
    edge E;
    ll v;
    vector<pll> count;
    vector<ll> group;
    
    ll dfs(ll w,ll depth,ll parent){
        if(count[w].F!=-1){return 1e18;}
        count[w]={depth,depth};
        bool p=true;
        for(int i=0;i<E[w].size();i++){
            if(E[w][i]==parent){
                if(!p){count[w].S=min(count[E[w][i]].F,count[w].S);}
                p=false;
            }
            else if(count[E[w][i]].F==-1){count[w].S=min(dfs(E[w][i],depth+1,w),count[w].S);}
            else{count[w].S=min(count[E[w][i]].F,count[w].S);}
        }
        return count[w].S;
    }
    
    void dfs2(ll where,ll g){
        group[where]=g;
        for(int i=0;i<E[where].size();i++){
            ll &to=E[where][i];
            if(is_bridge(where,to) || group[to]!=-1){continue;}
            dfs2(to,g);
        }
    }
    
public:
    Bridge(ll v,edge e={}):v(v),E(e){
        E.resize(v);
        count.resize(v,{-1,-1});
        group.resize(v,-1);
    }
    
    void add_edge(ll from,ll to){
        E[from].push_back(to);
        E[to].push_back(from);
    }
    
    void search(){
        dfs(0,1,-1);
    }
    
    bool is_bridge(ll u,ll v){
        if(count[u].F==count[v].F){return false;}
        if(count[u].F>count[v].F){swap(u,v);}
        if(count[u].F>=count[v].S){return false;}
        return true;
    }
    
    vector<pll> bridge(){
        vector<pll> ret;
        for(int i=0;i<v;i++){
            for(int t=0;t<E[i].size();t++){
                if(i<E[i][t] && is_bridge(i,E[i][t])){ret.push_back({i,E[i][t]});}
            }
        }
        return ret;
    }
    
    vector<ll> & Group(){
        ll count=0;
        for(int i=0;i<v;i++){
            if(group[i]==-1){dfs2(i,count); count++;}
        }
        return group;
    }
    
    void test(){
        for(int i=0;i<v;i++){
            cout<<"test "<<i<<" "<<count[i].F<<" "<<count[i].S<<endl;
        }
    }
};

vector<vector<pair<pll,pll>>> edge; //元の辺、行き先、重さ
vector<ll> node;
ll mi=E;
pll ans;
ll sum=0;

ll dfs(ll where,ll parent){
    ll count=0;
    for(auto &i:edge[where]){
        if(i.S.F==parent){continue;}
        ll W=dfs(i.S.F,where);
        if(pair<ll,pll>(mi,ans)>pair<ll,pll>(abs(sum-i.S.S-W-W),i.F)){
            mi=abs(sum-i.S.S-W-W);
            ans=i.F;
        }
        count+=W+i.S.S;
    }
    return count+=node[where];
}


int main(){
    ll v,e;
    cin>>v>>e;
    Bridge B(v);
    vector<pair<pll,ll>> Edge;
    for(int i=0;i<e;i++){
        ll u,v,w;
        cin>>u>>v>>w;
        u--; v--;
        if(u>v){swap(u,v);}
        B.add_edge(u,v);
        Edge.push_back({{u,v},w});
    }
    B.search();
    vector<ll> &G=B.Group();
    vector<pll> br=B.bridge();
    ll mx=-1;
    for(int i=0;i<G.size();i++){mx=max(mx,G[i]);}
    mx++;
    edge.resize(mx);
    node.resize(mx,0);
    mx=0;
    sort(Edge.begin(),Edge.end());
    for(int i=0;i<Edge.size();i++){
        pair<pll,ll> &w=Edge[i];
        sum+=w.S;
        if(B.is_bridge(w.F.F,w.F.S)){
            edge[G[w.F.F]].push_back({{w.F.F,w.F.S},{G[w.F.S],w.S}});
            edge[G[w.F.S]].push_back({{w.F.F,w.F.S},{G[w.F.F],w.S}});
        }
        else{
            node[G[w.F.S]]+=w.S;
            if(mx<w.S){
                mx=w.S;
                ans=w.F;
            }
        }
    }
    mi=sum-mx;
    dfs(0,-1);
    cout<<ans.F+1<<" "<<ans.S+1<<endl;
    
    
    
    return 0;
}