CF1108F MST Unification

You are given an undirected weighted connected graph with nn vertices and mm edges without loops and multiple edges.

The ii-th edge is ei=(ui,vi,wi)ei=(ui,vi,wi); the distance between vertices uiui and vivi along the edge eiei is wiwi (1≤wi1≤wi). The graph is connected, i. e. for any pair of vertices, there is at least one path between them consisting only of edges of the given graph.

A minimum spanning tree (MST) in case of positive weights is a subset of the edges of a connected weighted undirected graph that connects all the vertices together and has minimum total cost among all such subsets (total cost is the sum of costs of chosen edges).

You can modify the given graph. The only operation you can perform is the following: increase the weight of some edge by 11. You can increase the weight of each edge multiple (possibly, zero) times.

Suppose that the initial MST cost is kk. Your problem is to increase weights of some edges with minimum possible number of operations in such a way that the cost of MST in the obtained graph remains kk, but MST is unique (it means that there is only one way to choose MST in the obtained graph).

Your problem is to calculate the minimum number of operations required to do it.

Input

The first line of the input contains two integers nn and mm (1≤n≤2⋅105,n−1≤m≤2⋅1051≤n≤2⋅105,n−1≤m≤2⋅105) — the number of vertices and the number of edges in the initial graph.

The next mm lines contain three integers each. The ii-th line contains the description of the ii-th edge eiei. It is denoted by three integers ui,viui,vi and wiwi (1≤ui,vi≤n,ui≠vi,1≤w≤1091≤ui,vi≤n,ui≠vi,1≤w≤109), where uiui and vivi are vertices connected by the ii-th edge and wiwi is the weight of this edge.

It is guaranteed that the given graph doesn't contain loops and multiple edges (i.e. for each ii from 11 to mm ui≠viui≠vi and for each unordered pair of vertices (u,v)(u,v) there is at most one edge connecting this pair of vertices). It is also guaranteed that the given graph is connected.

Output

Print one integer — the minimum number of operations to unify MST of the initial graph without changing the cost of MST.

Examples

Input

8 10
1 2 1
2 3 2
2 4 5
1 4 2
6 3 3
6 1 3
3 5 2
3 7 1
4 8 1
6 2 4

Output

1

Input

4 3
2 1 3
4 3 4
2 4 1

Output

0

Input

3 3
1 2 1
2 3 2
1 3 3

Output

0

Input

3 3
1 2 1
2 3 3
1 3 3

Output

1

Input

1 0

Output

0

Input

5 6
1 2 2
2 3 1
4 5 3
2 4 2
1 4 2
1 5 3

Output

2

Note

The picture corresponding to the first example: 

You can, for example, increase weight of the edge (1,6)(1,6) or (6,3)(6,3) by 11 to unify MST.

The picture corresponding to the last example: 

You can, for example, increase weights of edges (1,5)(1,5) and (2,4)(2,4) by 11 to unify MST.

题目大意:给你一个图,你可以使得一些边权值变大(一次操作+1),使得最小生成树唯一且值不变的最少操作次数。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const int maxn=2e5+5;
int f[maxn],r[maxn],n,m;
struct node{
	int fr,to,cost;
	bool operator <(const node &x)const{
		return cost<x.cost;
	} 
};
vector<node> G;
void init(int n){
	for(int i=0;i<n+1;++i)	f[i]=i,r[i]=0;
}
int find(int x){
	if(x==f[x])	return f[x];
	else return f[x]=find(f[x]);
}
void unite(int x,int y){
	x=find(x),y=find(y);
	if(x==y)	return;
	if(r[x]<r[y])	f[x]=y;
	else{
		f[y]=x;
		if(r[x]==r[y])	r[y]++;
	}	
}
bool same(int x,int y){
	return find(x)==find(y);
}
int kruscal(){
	sort(G.begin(),G.end());
	int ans=0;
	for(int i=0;i<(int)G.size();){
		vector<node> tp;
		int j;
		for(j=i;j<(int)G.size()&&G[j].cost==G[i].cost;++j){
			if(!same(G[j].fr,G[j].to))	tp.push_back(G[j]);
		}
		i=j;
		for(j=0;j<(int)tp.size();++j){
			if(!same(tp[j].fr,tp[j].to))	unite(tp[j].fr,tp[j].to);
			else	ans++;
		}
	}
	return ans;
}
int main(){
	std::ios::sync_with_stdio(0);
	cin>>n>>m;
	init(n);
	int x,y,z;
	for(int i=1;i<=m;++i){
		cin>>x>>y>>z;
		G.push_back(node{x,y,z});
	}
	cout<<kruscal()<<endl;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值