F. MST Unification codeforces 1108F

8 篇文章 0 订阅
8 篇文章 0 订阅

传送门:https://codeforces.com/contest/1108/problem/F

F. MST Unification

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 canincrease 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 operationsin 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

Copy

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

Copy

1

input

Copy

4 3
2 1 3
4 3 4
2 4 1

output

Copy

0

input

Copy

3 3
1 2 1
2 3 2
1 3 3

output

Copy

0

input

Copy

3 3
1 2 1
2 3 3
1 3 3

output

Copy

1

input

Copy

1 0

output

Copy

0

input

Copy

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

output

Copy

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.

 

题意是我给你m个边和权值,然后你可以对任意一条边进行边权值++的操作,问你最少操作多少次,使得这个图的MST唯一。

我们首先应用krus的那种方法,想到合并,假设现在有很多边权值相同的边,那么我们很有可能因为这些边权相同的边 , 而生成很多个MST,思考如何让它唯一,遇到边权相同的边我们可以先假设把他们全部都加1,然后开始合并,当我们遇到当前这条边是在当前边权下最小的边,并且不连通,那么这条边我们是肯定会拿来生成MST的,那对于刚刚我们对这条边加的1的贡献就可以考虑去掉,然后依次进行这个操作即可,对于每个边权相同的边集,我们都这么跑一遍,就可以找到使得MST唯一所需要加的边权最小值。 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值