F. MST Unification

题目传送门

题意:

给你n个顶点,m条边;保证没有重边,其中存在多个MST(最小生成树),

你可以修改一些边的权值,让其中有且仅有一个最小生成树,求最少操作的边数。

思路:

最小生成树算法的加工,我们从kruskal算法入手,kruskal就是先对边排序,

然后遍历边不断加入一些合格边来完善最小生成树

那么在这个过程中,如果边的权值一样的话,就会产生多种MST,但是这里

不能仅仅只是累计相同权值的边数,因为与合格边相同权值的边可能可以选择

多条。

所以我们可以将所有符合的边累加起来,然后减去(n-1)就是与合格边冲突的边

,也就是答案了

#include<bits/stdc++.h>
using namespace std;
#define N 200005
int n,m;
struct node
{
int u,v,w;
bool operator <(const node &p) const{
return w<p.w;
}
}edge[N];
int fa[N]; int Find(int x)
{
if(x==fa[x]) return x;
else return fa[x]=Find(fa[x]);
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
for(int i=;i<m;i++)
scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].w);
for(int i=;i<=n;i++) fa[i]=i;
sort(edge,edge+m);
int ans=;
int L=;
for(int i=;i<m;i++)
{
int u=edge[i].u,v=edge[i].v;
u=Find(u),v=Find(v);
if(u!=v) ans++;
if(i+<m && edge[i].w!=edge[i+].w)
{
for(int j=L;j<=i;j++)
{
int u=edge[j].u,v=edge[j].v;
u=Find(u),v=Find(v);
if(u!=v) fa[u]=v;
}
L=i+;
}
}
printf("%d\n",ans-(n-));
}
}

Codeforces Round #535 (Div. 3) F的更多相关文章

  1. Codeforces Round #535 (Div. 3) 题解

    Codeforces Round #535 (Div. 3) 题目总链接:https://codeforces.com/contest/1108 太懒了啊~好久之前的我现在才更新,赶紧补上吧,不能漏掉 ...

  2. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  3. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  4. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  5. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  6. Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)

    题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...

  7. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  8. Codeforces Round #325 (Div. 2) F. Lizard Era: Beginning meet in the mid

    F. Lizard Era: Beginning Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  9. Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)

    题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...

随机推荐

  1. unity2017 光照与渲染(二)FAQs

    FAQ: 场景里的物体没有影子? 1)灯光是否开了影子 2)QualitySettings 中 shadows 的设置 3) 模型MeshRenderer 的 ReciveShadows 和 Cast ...

  2. postmaster - PostgreSQL多用户数据库服务器

    SYNOPSIS postmaster [ -A 0 | 1] [ -B nbuffers] [ -c name=value] [ -d debug-level] [ -D datadir] [ -F ...

  3. 一、bootstrap-upload

    一.bootstrap-upload 前端代码: @{ Layout = null; } <!DOCTYPE html> <html lang="zh-CN"&g ...

  4. nbench

    http://www.math.utah.edu/~mayer/linux/bmark.html nbench-byte-2.2.3  --> http://www.math.utah.edu/ ...

  5. Ansible笔记(2)---常用模块之文件操作

    一.copy模块 1.1作用: copy模块是将ansible主机上的文件拷贝到远程受控主机 1.2常用参数: src参数 :用于指定需要copy的文件或目录. dest参数 :用于指定文件将被拷贝到 ...

  6. 2018-10-01-weekly

    Algorithm 77. 组合 What 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. How 利用递归的思想,当凑够k个数时,就回退回去,remove掉一个数,在 ...

  7. k8s源码编译

    1.可在github上面直接下载源码 2.下载所需要的docker镜像,并打上tag,不知道tag,可以先编译一下,会提示所需image. docker pull index.alauda.cn/xu ...

  8. PHP与CI学习笔记

    CodeIgniter框架学习 安装 下载好包后,解压复制 system 和 application 目录到网站目录下 配置 配置目录 打开index.php设置好 $system_path . $a ...

  9. MAN VGEXTEND

    VGEXTEND(8)                                                        VGEXTEND(8) NAME/名称       vgexten ...

  10. tensorflow函数介绍 (5)

    1.tf.ConfigProto tf.ConfigProto一般用在创建session的时候,用来对session进行参数配置: with tf.Session(config=tf.ConfigPr ...