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>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

class graph_bridge{
public:
	static const int MAX=100005;
	vector<int> G[MAX];
	bool visited[MAX];
	int prenum[MAX],parent[MAX],lowest[MAX],timer;
	set<P> bridge;
	int cmp[MAX];

	void dfs(int v,int prev){
		prenum[v]=lowest[v]=timer;
		timer++;
		visited[v]=true;
		int next;
		for(int i=0;i<G[v].size();i++){
			next=G[v][i];
			if(!visited[next]){
				parent[next]=v;
				dfs(next,v);
				lowest[v]=min(lowest[v],lowest[next]);
				if(prenum[v]<lowest[G[v][i]])bridge.insert(P(min(v,G[v][i]),max(v,G[v][i])));
			}else if(next!=prev){
				lowest[v]=min(lowest[v],prenum[next]);
			}
		}
	}

	void dfs2(int v,int k){
		cmp[v]=k;
		for(int i=0;i<G[v].size();i++){
			int nv=G[v][i];
			if(cmp[nv]>=0)continue;
			if(bridge.find(P(min(v,nv),max(v,nv)))==bridge.end()){
				dfs2(nv,k);
			}
		}
	}

	int construct_TwoEdgeConnectedComponent(int N){
		for(int i=0;i<N;i++){
			visited[i]=false;
		}
		timer=1;
		for(int i=0;i<N;i++){
			if(!visited[i]){
				dfs(i,-1);
			}
		}
		memset(cmp,-1,sizeof(cmp));
		int k=0;
		for(int i=0;i<N;i++){
			if(cmp[i]==-1){
				dfs2(i,k);
				k++;
			}
		}
		return k;
	}
};

graph_bridge b;

int n,m;
int u[100005],t[100005];
ll w[100005];
ll nw[100005];
vector<P> G[100005];
map<P,int> mp;
ll dif=0;
P ans;
ll sum;

ll labs(ll v){
	if(v<0LL)return -v;
	return v;
}

ll dfs(int v,int p){
	ll val=nw[v];
	for(int i=0;i<G[v].size();i++){
		P e=G[v][i];
		if(e.first==p)continue;
		ll cv=dfs(e.first,v);
		val+=cv+w[e.second];
		ll cdif=labs(sum-cv*2LL-w[e.second]);
		//printf("%d %d %lld\n",v,e.first,cdif);
		if(cdif<dif){
			dif=cdif;
			ans=P(u[e.second],t[e.second]);
		}else if(cdif==dif){
			ans=min(ans,P(u[e.second],t[e.second]));
		}
	}
	//printf("%d %lld\n",v,val);
	return val;
}

int main(void){
	scanf("%d%d",&n,&m);
	sum=0;
	for(int i=0;i<m;i++){
		scanf("%d%d%lld",&u[i],&t[i],&w[i]);
		u[i]--;
		t[i]--;
		b.G[u[i]].push_back(t[i]);
		b.G[t[i]].push_back(u[i]);
		mp[P(u[i],t[i])]=i;
		sum+=w[i];
		if(dif<w[i]){
			dif=w[i];
			ans=P(u[i],t[i]);
		}else if(dif==w[i]){
			ans=min(ans,P(u[i],t[i]));
		}
	}
	dif=sum-dif;
	//printf("%lld\n",dif);
	int siz=b.construct_TwoEdgeConnectedComponent(n);
	for(int i=0;i<m;i++){
		if(b.cmp[u[i]]==b.cmp[t[i]]){
			nw[b.cmp[u[i]]]+=w[i];
		}
	}
	for(set<P>::iterator it=b.bridge.begin();it!=b.bridge.end();it++){
		P p=*it;
		//printf("b %d %d\n",p.first,p.second);
		int ev=mp[p];
		p.first=b.cmp[p.first];
		p.second=b.cmp[p.second];
		G[p.first].push_back(P(p.second,ev));
		G[p.second].push_back(P(p.first,ev));
	}
	dfs(0,-1);
	printf("%d %d\n",ans.first+1,ans.second+1);
	return 0;
}