Codeforces Round #535 F-MST Unification
题目大意:
给定n m 为图中的点数n和边数m
给定m条边的信息 u v w 为u点到v点有一条长度为w的边
图中无环无重边
这个图的MST的花费为k 但可能存在多种花费为k的MST的方案
此时对图中的边进行操作 可增大权重或翻倍增大权重
要求只保留图中的一种花费为k的MST方案时 需要对最少多少条边进行操作
1.原本的kruskal 会选出构成MST的最短的n-1条边
即在n=5边长度为 1 2 2 3 4 4 5 6 的图中
原本会选出4条 1 2 3 4 (能构成1种MST的方案)
2.这个方法会记录到最短的n-1条边和与这些边长度相等的边
现在会选出6条 1 2 2 3 4 4 (能构成多种MST方案)
3.此时只保留一种MST方案的话需4条(n-1条)边
则需要对多出的6-4=2条边进行操作
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int N=2e5+;
int n, m;
struct EDGE {
int u,v,w;
bool operator <(const EDGE& p)const {
return w<p.w;
}
}e[N];
int fa[N];
int getfa(int u) {
if(fa[u]==u) return u;
else return fa[u]=getfa(fa[u]);
}
int main()
{
while(~scanf("%d%d",&n,&m)) {
for(int i=;i<m;i++)
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
sort(e,e+m);
for(int i=;i<=n;i++) fa[i]=i;
int ans=, L=;
for(int i=;i<m;i++) {
int u=getfa(e[i].u), v=getfa(e[i].v);
if(u!=v) ans++; if(i+<m and e[i+].w!=e[i].w) {
for(int j=L;j<=i;j++) {
u=getfa(e[j].u), v=getfa(e[j].v);
if(u!=v) fa[u]=v;
}
L=i+;
}
}
printf("%d\n",ans-(n-));
/// 去掉唯一的的MST所需的n-1条边
} return ;
}
Codeforces Round #535 F-MST Unification的更多相关文章
- Codeforces Round #535 (Div. 3) F
F. MST Unification 题目传送门 题意: 给你n个顶点,m条边:保证没有重边,其中存在多个MST(最小生成树), 你可以修改一些边的权值,让其中有且仅有一个最小生成树,求最少操作的边数 ...
- Codeforces Round #535 (Div. 3) 题解
Codeforces Round #535 (Div. 3) 题目总链接:https://codeforces.com/contest/1108 太懒了啊~好久之前的我现在才更新,赶紧补上吧,不能漏掉 ...
- Codeforces Round #535 (Div. 3) [codeforces div3 难度测评]
hhhh感觉我真的太久没有接触过OI了 大约是前天听到JK他们约着一起刷codeforces,假期里觉得有些颓废的我忽然也心血来潮来看看题目 今天看codeforces才知道居然有div3了,感觉应该 ...
- Educational Codeforces Round 40 F. Runner's Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
- Codeforces Round #535(div 3) 简要题解
Problem A. Two distinct points [题解] 显然 , 当l1不等于r2时 , (l1 , r2)是一组解 否则 , (l1 , l2)是一组合法的解 时间复杂度 : O(1 ...
- Codeforces Round #535 E2-Array and Segments (Hard version)
Codeforces Round #535 E2-Array and Segments (Hard version) 题意: 给你一个数列和一些区间,让你选择一些区间(选择的区间中的数都减一), 求最 ...
- Codeforces Round #535 (Div. 3) 解题报告
CF1108A. Two distinct points 做法:模拟 如果两者左端点重合就第二条的左端点++就好,然后输出左端点 #include <bits/stdc++.h> usin ...
- Educational Codeforces Round 61 F 思维 + 区间dp
https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...
- Educational Codeforces Round 51 F. The Shortest Statement(lca+最短路)
https://codeforces.com/contest/1051/problem/F 题意 给一个带权联通无向图,n个点,m条边,q个询问,询问两点之间的最短路 其中 m-n<=20,1& ...
随机推荐
- python 根据字典的键值进行排序
1.利用key排序 d = {'d1':2, 'd2':4, 'd4':1,'d3':3,} for k in sorted(d): print(k,d[k]) d1 2d2 4d3 3d4 1 2. ...
- Set 对象和WeakSet对象
Set对象是值的集合,你可以按照插入的顺序迭代它的元素. Set中的元素只会出现一次,即 Set 中的元素是唯一的,一种有效去重方式. , , , , ]); console.log(set1.has ...
- sudo 出现unable to resolve host hostname 解决方法
Linux 环境,我的电脑叫枝桠(机器的hostname), 每次执行sudo 就出现这个警告讯息: sudo: unable to resolve host 枝桠 直接修改 /etc/hosts 的 ...
- JavaScript中的对象-创建对象的7种模式
文章来源:http://blog.csdn.net/u014346301/article/details/52204967 ECMA-262把对象定义为:”无需属性的集合,其属性可以包含基本值.对象或 ...
- windows上测试网络数据跳转路径
今天遇到一个有关路由方面的问题,所以向一位CCIE大神问了点问题.就是关于ip ping包出了pc之后怎么走的. 然后告诉我了这么一个命令: Tracert(跟踪路由)是路由跟踪实用程序,用于确定 I ...
- GetWindowsDirectoryA and GetSystemDirectory
#include <iostream> #include <Windows.h> using std::cout; using std::endl; // 获取Windows文 ...
- 笔记59 Spring+Hibernate整合(二)
一.项目结构 二.创建表 数据库中只有一张表,stock,三个字段:stock_id.stock_code和stock_name. CREATE TABLE `stock` ( `STOCK_ID` ...
- vue撸的demo
之前做过一个layui+flask的测试平台,后来听说vue挺火,学了下,做个记录 一.想法 主要想开发几大功能,方便以后复用 1.工作中常常需要记录知识,所以我决定会做一个类似markdown笔记的 ...
- codeforces round 433 D. Jury Meeting
题目大意: 输入n,m,k,分别代表城市的数量,城市编号1~n,航班的数量以及会议必须所有人员到会一起商议的天数,然后及时输入m行航班的信息,每一行输入d,f,t,c分别表示航班到站和始发的那一天(始 ...
- 【leetcode】973. K Closest Points to Origin
题目如下: We have a list of points on the plane. Find the Kclosest points to the origin (0, 0). (Here, ...