【Kruskal】1108F MST Unification

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.

题目链接:http://codeforces.com/contest/1108/problem/F

题目大意:给一个无向加权连通图,你每次可以选择一条边+1在保证最小生成树的值不变的情况下,生成树唯一.求最小+1的次数

 

思路:首先要明确一点,每条边最多只能加一次.因此不难得出答案便等于构造MST时冲突边的个数.比如有权值相等e1 e2的两条边.当e1和e2分别单独加入MST时不会构成圈,但是如果e1 e2一起加入MST时会构成圈,我们便认为这2个点冲突,只能选其一.明确后我们便可以用kruskal解决了

代码:

#include <bits/stdc++.h>

#define Fi first
#define Se second
using namespace std;
const int N=2e5+10;
struct edge
{
    int u,v,w;
    void set(){scanf("%d%d%d",&u,&v,&w);}
}a[N];
inline bool cmp(edge &a,edge &b)
{
    return a.w<b.w;
}
int f[N];
inline int find_f(int rt){return rt==f[rt]?rt:f[rt]=find_f(f[rt]);}
vector<pair<int,int> >vc;
int main()
{
    int n,m;scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) f[i]=i;
    for(int i=1;i<=m;i++)
    {
        a[i].set();
    }
    sort(a+1,a+1+m,cmp);
    int ans=0;
    for(int i=1;i<=m;i++)
    {
        int j=i;vc.clear();
        while(j<=m&&a[i].w==a[j].w)
        {
            int fa=find_f(a[j].u);
            int fb=find_f(a[j].v);
            j++;
            if(fa==fb) continue;//删除对最小生成树没有影响的边
            vc.push_back({fa,fb});
        }
        for(int j=0;j<vc.size();j++)
        {
            int fa=find_f(vc[j].Fi);
            int fb=find_f(vc[j].Se);
            if(fa==fb) ans++;//如果在相等则可以认为冲突
            else f[fa]=fb;
        }
        i=j-1;
    }
    cout<<ans<<endl;
    return 0;
}
/*

*/

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值