Codeforces Round #535 (Div. 3) F
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的更多相关文章
- Codeforces Round #535 (Div. 3) 题解
Codeforces Round #535 (Div. 3) 题目总链接:https://codeforces.com/contest/1108 太懒了啊~好久之前的我现在才更新,赶紧补上吧,不能漏掉 ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- 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 ...
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...
随机推荐
- PTA 错题记录
程设期中考, 记录一下曾经做错的选择填空. 1. 2. 3. 4. 5. 6.
- 2014-03-01 春季PAT 1073-1076解题报告
今天下午的PAT考试状态不理想,回来怒刷了一遍,解题报告如下: 1073. Scientific Notation (20) 基本模拟题,将一长串的科学计数转换为普通的数字表示方式.思路是是数组存储输 ...
- poj 2689 Prime Distance(区间筛选素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9944 Accepted: 2677 De ...
- ltp-ddt eth iperf
ETH_S_PERF_IPERF_TCP_8K_1448B source 'common.sh'; run_iperf.sh -m -M 1500 -f M -d -t 60 -w 8K run_ip ...
- web项目分层设计
model.dao.service.controller之间的关系,还有util和task的简介 model: 与数据库中的表一一对应,实现set和get的方法.
- a[b]和b[a]区别
#include <stdio.h> main() { char a[5] = "abcd"; int b = 3; printf("%c\n",a ...
- 【leetcode】1090. Largest Values From Labels
题目如下: We have a set of items: the i-th item has value values[i] and label labels[i]. Then, we choose ...
- OPTIONS请求后台处理 跨域Filter
import cn.hutool.http.Method; import org.springframework.web.filter.OncePerRequestFilter; import jav ...
- 英语单词composing
composing 来源——书籍Python.Crash.Course.2015.11 Using Individual Values from a List You can use individu ...
- HTML基础入门学习
上一篇给大家介绍了学习HTML的准备工作,本文开始带大家步入HTML的学习 一.HTML基础 网页的组成: HTML:页面构成 css:页面样式表现 JavaScript:交互行为 HTML简介: H ...