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. 2018-8-10-C#-TimeSpan-时间计算

    title author date CreateTime categories C# TimeSpan 时间计算 lindexi 2018-08-10 19:16:51 +0800 2018-06-1 ...

  2. Q开头的类找不到,无法加载插件:com.mysema.maven:apt-maven-plugin

    http://www.jspxcms.com/documentation/297.html 如果出现无法加载com.mysema.maven:apt-maven-plugin插件的情况,通常是由于ma ...

  3. [NOI2004]郁闷的出纳员(平衡树)

    [NOI2004]郁闷的出纳员 题目链接 题目描述 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的 ...

  4. 2018-10-01-weekly

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

  5. alert(1) to win 15

  6. mysql版本

    $ mysql Welcome to the MariaDB monitor.  Commands end with ; or \g. Your MySQL connection id is 4791 ...

  7. Xcode编辑器之快捷键的使用

    一,快捷键图标 图标 键盘 ⌘ Command ⌃ Control ⌥ Option ⇧ Shift 二, 常用快捷键 文件快捷键 快捷键 键盘  描述 ⌘N  command + N 新文件 ⇧⌘N ...

  8. Jquery取得Iframe中的元素

    DOM方法: 父窗口操作IFRAME:window.frames["iframeSon"].documentIFRAME操作父窗口: window.parent.document ...

  9. Gym 100917F Find the Length

    题目链接:http://codeforces.com/gym/100917/problem/F ---------------------------------------------------- ...

  10. 奖项-MVP:MVP(微软最有价值专家)百科

    ylbtech-奖项-MVP:MVP(微软最有价值专家)百科 微软最有价值专家(MVP) 是指具备一种或多种微软技术专业知识,并且积极参与在线或离线的社群活动,经常与其他专业人士分享知识和专业技能,受 ...