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. mysql查询时间戳转换

    mysql查询时间戳转换 SELECT FROM_UNIXTIME(create_time) FROM tablename; 更新时间为七天以后 UPDATE t_rebate_trade_item ...

  2. Python日志库logging总结

    转自  https://cloud.tencent.com/developer/article/1354396 在部署项目时,不可能直接将所有的信息都输出到控制台中,我们可以将这些信息记录到日志文件中 ...

  3. C++数组读入MATLAB数据

    data = rand(8, 10); fid = fopen('File.data', 'w'); if fid == - 1 error('Cannot open file for writing ...

  4. 【leetcode】1041. Robot Bounded In Circle

    题目如下: On an infinite plane, a robot initially stands at (0, 0) and faces north.  The robot can recei ...

  5. [原创] Delphi Win API函数 操作帮助文件 HtmlHelpA函数介绍

    Delphi Win API函数 操作帮助文件 HtmlHelpA函数介绍 函数原型:HWND HtmlHelpA( HWND hwndCaller, LPCSTR pszFile, UINT uCo ...

  6. 继续写高精!noip2012国王游戏。。。

    国王游戏 题目描述: 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成一排,国王 ...

  7. [USACO17JAN]Balanced Photo平衡的照片 (树状数组)

    题目链接 Solution 先离散化,然后开一个大小为 \(100000\) 的树状数组记录前面出现过的数. 然后查询 \((h[i],n]\) 即可. 还要前后各做一遍. Code #include ...

  8. UPDATE 在不同数据库中的使用方式

    MYSQL 中update 表一 set Gmoney = 表二.列名 from 表一,表二 where 表一.EMPID = 表二.EMPID举例:update table1 set table1. ...

  9. [CSP-S模拟测试]:Median(暴力+模拟)

    题目描述 定义两个数列: $$S=\{S(1),S(2),...,S(n)\}\text{和}S_2\{S_2(1),S_2(2),...,S_2(n)\}$$ $$S(k)=(p_k\times k ...

  10. 一个关于 ie 浏览器的 bug 解决过程和思考

    首先我们测试了老师反馈的异常情况.这所中学使用的是 IE8 浏览器.IE8 浏览器提交作文评分的情况是:一直停留在“正在提交系统评分”的页面,停留了很长时间以后,页面空白. 换用火狐浏览器,可以正常评 ...