codeforce893C (并查集)
题意描述
Vova promised himself that he would never play computer games… But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.
Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.
Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants c i gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.
The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?
Take a look at the notes if you think you haven’t understood the problem completely.
Input The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.
The second line contains n integer numbers c i (0 ≤ c i ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.
Then m lines follow, each containing a pair of numbers ( x i, y i) which represent that characters x i and y i are friends (1 ≤ x i, y i ≤ n, x i ≠ y i). It is guaranteed that each pair is listed at most once.
Output Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
思路
我们可以使用并查集来维护在同一集合内的最少费用,最后再遍历一次集合,加上所有的最小值即可
AC代码
#include<bits/stdc++.h>
#define x first
#define y second
#pragma GCC optimize(2)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize(3)
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("sse3","sse2","sse")
#pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3")
#pragma GCC target("f16c")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#pragma GCC diagnostic error "-std=c++14"
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=1e5+10;
const int M=1e6;
const int INF=0x3f3f3f3f;
const int MOD=1000000007;
int n,m;
int p[N],cost[N];
bool st[N];
int a[N];
void init(int n){
for(int i=1;i<=n;i++){
p[i]=i;
cost[i]=a[i];
st[i]=false;
}
}
int Find(int x){
if(p[x]!=x) p[x]=Find(p[x]);
return p[x];
}
int main()
{
IOS;
while(cin>>n>>m){
for(int i=1;i<=n;i++) cin>>a[i];
init(n);
for(int i=1;i<=m;i++){
int x,y;cin>>x>>y;
cost[Find(y)]=min(cost[Find(y)],cost[Find(x)]);
p[Find(x)]=Find(y);
}
LL ans=0;
for(int i=1;i<=n;i++){
if(st[Find(i)]) continue;
ans+=(LL)cost[Find(i)];
st[Find(i)]=true;
}
cout<<ans<<endl;
}
return 0;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 前端必备技能:json-server全攻略
- 温故知新——Spring AOP(二)
- R语言中的偏最小二乘PLS回归算法
- R如何与Tableau集成分步指南
- 漏洞复现 | CVE‐2020‐5902踩坑记
- PCA图显示分组无差异,怎么办?
- 我接手了一个“垃圾”系统,全栈优化后将性能提升了350倍
- 用 eggnog-mapper 进行功能注释
- 将SAP C4C Custom BO使用ABSL编写的逻辑通过OData服务暴露出去
- Linux笔记(14)| 字符设备驱动基础入门(续)
- scRNAseq包更新动作太大
- Linux笔记(13)| 字符设备驱动基础入门
- 你要的rmarkdown文献图表复现全套代码来了(单细胞)
- 祖传的单个10x样本的seurat标准代码
- 浏览器输入某URL后,HTTP开启了一段奇妙之旅!