CF1108F.MST Unification

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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)e_i = (u_i, v_i, w_i); the distance between vertices uiu_i and viv_i along the edge eie_i is wiw_i (1≤wi1 \le w_i). 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.

给你一个包含 nn 个顶点和 mm 条边的无向带权连通图,图中不含自环和重边。

第 ii 条边为 ei=(ui,vi,wi)e_i = (u_i, v_i, w_i);沿边 eie_i 连接顶点 uiu_i 和 viv_i 的距离为 wiw_i(其中 1≤wi1 \le w_i)。该图是连通的,即对任意一对顶点,均存在一条仅由图中边构成的路径连接它们。

在边权均为正数的情况下,最小生成树(MST) 是原图边集的一个子集,它连接所有顶点,且在所有满足该性质的子集中总权重(即所选边的权重之和)最小。

你可以对给定图进行修改。唯一允许的操作是:将某条边的权重增加 11。每条边可执行该操作任意多次(包括零次)。

假设初始图的 MST 总权重为 kk。你的任务是:以最少的操作次数,增加若干条边的权重,使得所得图的 MST 总权重仍为 kk,但 MST 唯一(即所得图中仅存在唯一一种选择 MST 的方式)。

你需要计算完成该目标所需的最少操作次数。

输入格式

The first line of the input contains two integers nn and mm (1≤n≤2⋅105,n−1≤m≤2⋅1051 \le n \le 2 \cdot 10^5, n - 1 \le m \le 2 \cdot 10^5) — 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 eie_i. It is denoted by three integers ui,viu_i, v_i and wiw_i (1≤ui,vi≤n,ui≠vi,1≤w≤1091 \le u_i, v_i \le n, u_i \ne v_i, 1 \le w \le 10^9), where uiu_i and viv_i are vertices connected by the ii-th edge and wiw_i 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≠viu_i \ne v_i 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.

输入的第一行包含两个整数 nn 和 mm(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5,n−1≤m≤2⋅105n - 1 \le m \le 2 \cdot 10^5),分别表示初始图中的顶点数和边数。

接下来的 mm 行每行包含三个整数。第 ii 行描述第 ii 条边 eie_i,由三个整数 ui,viu_i, v_i 和 wiw_i(1≤ui,vi≤n1 \le u_i, v_i \le n,ui≠viu_i \ne v_i,1≤wi≤1091 \le w_i \le 10^9)组成,其中 uiu_i 和 viv_i 是第 ii 条边所连接的两个顶点,wiw_i 是该边的权重。

保证给定图中不含自环和重边(即对每个 ii(1≤i≤m1 \le i \le m)均有 ui≠viu_i \ne v_i,且对任意无序顶点对 (u,v)(u, v),至多存在一条边连接该对顶点)。同时保证给定图是连通的。

输出格式

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

输出一个整数——在不改变初始图的最小生成树(MST)代价的前提下,使该图的 MST 唯一所需的最少操作次数。

输入输出样例

  • 输入#1

    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

    输出#1

    1
  • 输入#2

    4 3
    2 1 3
    4 3 4
    2 4 1

    输出#2

    0
  • 输入#3

    3 3
    1 2 1
    2 3 2
    1 3 3

    输出#3

    0
  • 输入#4

    3 3
    1 2 1
    2 3 3
    1 3 3

    输出#4

    1
  • 输入#5

    1 0

    输出#5

    0
  • 输入#6

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

    输出#6

    2

说明/提示

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,6)(1, 6) 或 (6,3)(6, 3) 的权值增加 11,从而使最小生成树(MST)唯一。

最后一个示例对应的图片:

例如,你可以将边 (1,5)(1, 5) 和 (2,4)(2, 4) 的权值各增加 11,从而使最小生成树(MST)唯一。

输入解题思路,AI测评打分。不知道怎么写?

首页