题源CF-1108

CF-1108-MST Unification

Description
You are given an undirected weighted connected graph with n vertices and m edges without loops and multiple edges.
The i-th edge is ei=(ui,vi,wi); the distance between vertices ui and vi along the edge ei is wi (1≤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 1. You can increase the weight of each edge multiple (possibly, zero) times.
Suppose that the initial MST cost is k. 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 k, 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 n and m (1≤n≤2⋅105,n−1≤m≤2⋅105) — the number of vertices and the number of edges in the initial graph.
The next m lines contain three integers each. The i-th line contains the description of the i-th edge ei. It is denoted by three integers ui,vi and wi (1≤ui,vi≤n,ui≠vi,1≤w≤109), where ui and vi are vertices connected by the i-th edge and wi is the weight of this edge.
It is guaranteed that the given graph doesn’t contain loops and multiple edges (i.e. for each i from 1 to m ui≠vi and for each unordered pair of vertices (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) or (6,3) by 1 to unify MST.
The picture corresponding to the last example:

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

题意

  • 多组输入,每组先输入一个N和M,分别代表顶点数和边数
  • 接下来给出M条边连通的两个点坐标,以及连通长度
  • 题意所给的图为连通图,每条边可对边权进行+1的操作
  • 在不改变原图最小生成树边权和的前提下,进行几次操作,可以使得新图的最小生成树唯一
  • 每组数据输出对应的操作次数

题解

  • 这是最小生成树模板题,对模板进行微改即可(思维上略不同于模板的直接思维)
  • 如果最小生成树不唯一,那么图中肯定存在了多条权值相同的边
  • 进入函数前,已经对边按照权值进行了升序,所以关键是遇到边权相同的边如何处理
  • 函数内部用cnt统计要操作多少条边,容易疏忽的点在于,边权相同不一定只取其一
  • 如果边权和之前的相同,但是这条边算上也不会造成回路,那么相对于别的更长的边,为何不取这条短的呢
  • 只有边权与之前相同,如果都连通,就会造成回路时,取其一来连通
  • 同时,题意给出,所给定的图都为连通图,故如果边权相同+连通造成回路,直接操作+1舍弃这条边即可
  • 多选一的边本身就只能选其一不多选一的边依旧继续
  • 那么多选一后没被选中的边本身也用不到,所以操作后不会对后续产生影响

详见下图三个关键形态的思维点,以及代码行的注释

涉及知识点

  • 最小生成树MST 算法(此题用到Kruskal算法)
  • 对于最小生成树的算法-详见链接博客介绍最小生成树

AC代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=2e5+10;
int father[maxn];
int n,m,cnt,key;
struct node
{int st;int ed;int w;
}edge[maxn];
bool cmp(node x,node y)
{return x.w<y.w;
}
void init()
{for(int i=1;i<=n;i++) father[i]=i;memset(edge,0,sizeof(edge));
}
int find(int x)
{return x==father[x]?x:father[x]=find(father[x]);
}
void kruskal()
{int i;cnt=0;//统计操作次数for(i=1;i<=m;++i){for(key=i;edge[i].w==edge[key].w;++key);//遍历得到从何处开始边权不同for(int j=i;j<key;++j){int fx=find(edge[j].st);int fy=find(edge[j].ed);if(fx!=fy) cnt++;//连通成环则不可取,不必操作;不成环的假设操作 }for(int j=i;j<key;++j){int fx=find(edge[j].st);int fy=find(edge[j].ed);if(fx!=fy){father[fx]=father[fy];/*第i个位置先被遍历,满足if,其实这是重复序列的第一个自己和自己重复,实际是不能算是重复所以自己这一遍操作肯定不用,于是进行cnt--; */cnt--;/*按排序后的顺序一个个遍历排在前面且满足条件的已经连通现在这个edge[j].st和edge[j].ed连通后依旧不成环(非同祖先)全连不环都要取,别误操作 ,于是进行cnt--; */}}/*实际只有之前的连通,现在的假设连通会成环(现在两点同祖先)时 两种连通方式二选一,会导致多种形态的最小生成树但之前同边权的已经按照边权进行过连通这个的边权+1对整体最小值不变,因而可进行+1的操作题目保证给定图是连通的,即保证了“全连成环选其一”要操作的进行一次+1就够啦 */ i=key-1;//此处-1是因为在for循环内部有++i,还会加回来的 //从 i 到 key-1 的边权都是相等的,处理完后接下来从 key 开始处理即可 }
}
int main()
{while(~scanf("%d %d",&n,&m)){init();for(int i=1;i<=m;i++) scanf("%d %d %d",&edge[i].st,&edge[i].ed,&edge[i].w);sort(edge+1,edge+m+1,cmp);//对边按照权值进行排序kruskal();printf("%d\n",cnt);}
}

#2020.02.05训练题解#最小生成树入门(F题)相关推荐

  1. #2020.02.04训练题解#背包入门(E题)

    题源HDU-1248 HDU-1248-寒冰王座 Description 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道 ...

  2. 纪中集训2020.02.05【NOIP提高组】模拟B 组总结反思——【佛山市选2010】组合数计算,生成字符串 PPMM...

    目录 JZOJ2290. [佛山市选2010]组合数计算 比赛时 之后 JZOJ2291. [佛山市选2010]生成字符串 比赛时 之后 JZOJ2292. PPMM 比赛时 之后 JZOJ2290. ...

  3. 2020牛客国庆集训派对day2 F题 Java大数处理

    题目: 链接:https://ac.nowcoder.com/acm/contest/16913/F 来源:牛客网 The following code snippet calculates the ...

  4. 2020年TI杯电子设计大赛F题及2022年山西省电子设计大赛E题简易无接触温度测量与身份识别装置整体思路及代码

    在2022年山西省电子设计大赛选题刚刚公布的时候,我们组本来是想做电源题的,奈何学艺不精,最后转做了这道仪器仪表题,经过几天紧张的制作,写报告,最终也是获得了省一,想要代码和报告文档的可以私聊我或者从 ...

  5. 中国石油大学 2019-2020大中小学训练赛第二场 F题 位置 【螺旋矩阵+DFS】

    问题链接 http://icpc.upc.edu.cn/problem.php?cid=1961&pid=5 题目描述 由于晨晨还没有研究出核心算法,在游戏中总是被明明击败.晨晨拿出了杀手锏进 ...

  6. 2020年中国研究生数学建模竞赛F题——飞行器质心平衡供油策略优化研究

  7. 2020年第十七届数模竞赛F题 飞行器质心平衡供油策略优化 建模【分享交流】

    飞行器质心平衡供油策略优化 导航不迷路 2020研究生华为杯数学建模比赛题目 题目A:芯片相噪算法 题目B:汽油辛烷值建模 题目C:面向康复工程的脑信号分析和判别建模 题目D:无人机集群协同对抗 题目 ...

  8. 2020TI杯全国大学生电子设计大赛F题解决方案视觉部分

    完整代码库: https://github.com/bossConneR/K210_FACEDET star以跟进最新更新 K210_FACEDET 2020年全国大学生电子设计竞赛F题视觉部分解决方 ...

  9. NEFU 大一寒假训练十二(set)2020.02.18

    Summary 可能是昨天的题少了一些,今天的题多了一堆,还疯狂TLE /(ㄒoㄒ)\~~ Information No. Title AC/Submit A 明明的随机数-set 60/101 B ...

最新文章

  1. 『互联网架构』软件架构-spring源码之spring结构概述
  2. promise和Angular中的 $q, defer
  3. 饿了么多活利器:实时双向复制工具(DRC)
  4. MySQL复习资料(四)——MySQL-聚合函数
  5. git 回退远端master分支版本
  6. 上市开放式基金(LOF)
  7. java 动态绑定原理_详解Java动态绑定机制的内幕(图)
  8. 如何暂停一个正在运行的线程?
  9. Github Actions
  10. 详解Python线程对象daemon属性对线程退出的影响
  11. 【Android开发经验】Android相关问题的好文章整理——温故而知新,可以为师矣
  12. 黑马程序员14套经典IT教程+面试宝典
  13. 2021-03-21 什么是鲁棒性?
  14. Linux启蒙之系统裁剪(一)
  15. 进入BeOS的花花世界 系列六
  16. InputStream 、 InputStreamReader 、 BufferedReader
  17. 用户权限影响瑞星安装
  18. Siemens TIA14 安装问题汇总
  19. 基于机智云平台的泵站智能巡检系统
  20. 计算机应用模块等级考试大纲,计算机等级考试大纲最新.doc

热门文章

  1. JavaWeb相关知识点思维导图
  2. ipad如何阅读html文件格式,ipad HTML文件怎么打开
  3. SitePoint播客#22:Bing的Boondoggles
  4. mac homebrew安装使用
  5. CSS3 对文本的基本设置大全
  6. Andorid-的面试题
  7. 激活win10 2016企业版
  8. align_corners 的意义
  9. android项目开发教程,Android项目开发教程
  10. Android:Android系统中安装的数据文件夹在哪个位置,如何删除wps下载下来的文件