Educational Codeforces Round 33 (Rated for Div. 2) C题·(并查集变式)
C. Rumor
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 ci 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 ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.
Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). 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.
Input
Output
思路:明显的并查集。
AC代码:
#include<bits/stdc++.h>
using namespace std;
#define N 100009
#define int long long
int cost[N];
int f[N];
int getf(int v){
if(v==f[v]){
return f[v];
}else{
f[v]=getf(f[v]);
return f[v];
}
}
void merge(int u,int v){
int t1=getf(u);
int t2=getf(v);
if(t1!=t2){
f[t2]=t1;
}
}
signed main(){
int n,m;
cin>>n>>m;
;
;
;i<=n;i++){
scanf("%lld",&cost[i]);
sb+=cost[i];
f[i]=i;
}
){
cout<<sb<<endl;
;
}
;i<m;i++){
int x,y;
scanf("%lld%lld",&x,&y);
merge(x,y);
}
/*for(int i=1;i<=n;i++){
if(i==f[i]){
cout<<i<<" ";
}
}
cout<<endl;*/
;i<=n;i++){
cost[getf(i)]=min(cost[getf(i)],cost[i]);// 关键:将该每一个集合中的最小花费赋值给祖先。
}//
;i<=n;i++){
if(i==f[i]){
ans+=cost[i];
}
}
printf("%lld\n",ans);
;
}
Educational Codeforces Round 33 (Rated for Div. 2) C题·(并查集变式)的更多相关文章
- Educational Codeforces Round 33 (Rated for Div. 2) D题 【贪心:前缀和+后缀最值好题】
D. Credit Card Recenlty Luba got a credit card and started to use it. Let's consider n consecutive d ...
- Educational Codeforces Round 33 (Rated for Div. 2) B题
B. Beautiful Divisors Recently Luba learned about a special kind of numbers that she calls beautiful ...
- Educational Codeforces Round 33 (Rated for Div. 2) A题
A. Chess For Three Alex, Bob and Carl will soon participate in a team chess tournament. Since they a ...
- Educational Codeforces Round 78 (Rated for Div. 2)D(并查集+SET)
连边的点用并查集检查是否有环,如果他们的fa是同一个点说明绕了一圈绕回去了.n个点一共能连n-1条边,如果小于n-1条边说明存在多个联通块. #define HAVE_STRUCT_TIMESPEC ...
- Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays
题目链接 题意:给你两个数x,yx,yx,y,让你构造一些长为yyy的数列,让这个数列的累乘为xxx,输出方案数. 思路:考虑对xxx进行质因数分解,设某个质因子PiP_iPi的的幂为kkk,则这个 ...
- Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)
题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...
- Educational Codeforces Round 33 (Rated for Div. 2) 题解
A.每个状态只有一种后续转移,判断每次转移是否都合法即可. #include <iostream> #include <cstdio> using namespace std; ...
- Educational Codeforces Round 33 (Rated for Div. 2)A-F
总的来说这套题还是很不错的,让我对主席树有了更深的了解 A:水题,模拟即可 #include<bits/stdc++.h> #define fi first #define se seco ...
- Educational Codeforces Round 33 (Rated for Div. 2) D. Credit Card
D. Credit Card time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
随机推荐
- spark调优篇-spark on yarn web UI
spark on yarn 的执行过程在 yarn RM 上无法直接查看,即 http://192.168.10.10:8088,这对于调试程序很不方便,所以需要手动配置 配置方法 1. 配置 spa ...
- 移动端测试之APP安全测试
现在APP测试已经是测试行业的一个重要分支,对APP测试技能和经验的要求也越来越高,看到一篇关于APP安全测试的总结,分享给需要的朋友.1.软件权限1)扣费风险:包括发送短信.拨打电话.连接网络等2) ...
- Jobs(三) HTML的form表单提交中文后,后台取出乱码的问题
解决form表单中提取的中文在后台乱码的问题有两种情况: form表单以GET方式提交: form表单以POST方式提交 一. 解决以GET方式提交的中文乱码问题,可以更改Tomcat服务器的配置文件 ...
- SvcUtil.exe工具生成客户端代理类
1.以管理员身份运行vs下命令工具: 2.运行代码示例:C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin>svcutil http: ...
- JS基础_函数的返回值
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- div 清除浮动的四种方法
概述:为了解决父级元素因为子级内部高度为0的问题 (很多情况 不方便给父级元素高,因为不知道有多少内容,让里面的盒子自动撑起高度),清除浮动本质叫闭合浮动更好一些,清除浮动就是把浮动的盒子关到里面,让 ...
- mORMot学习笔记 (一)
官方网站:https://synopse.info/fossil/wiki/Synopse+OpenSource 下载地址:https://synopse.info/fossil/wiki?name= ...
- vue-element-admin 之设置侧边栏的icon
一.将icon的svg文件放置如下图文件位置 二.在路由设置中设置(icon设置为svg的文件名称即可) 注:若icon选中的颜色不会随着侧边栏文字颜色变动,把svg文件中的fill ...
- GOLANG接口编程的最佳实践一 (sort.Sort(data Interface ) )
package main import( "fmt" "sort" "math/rand" ) //定义一个武当派的结构体 type Wud ...
- RHEL6进入救援模式
1.救援模式 救援模式作用: 更改root密码: 恢复硬盘.文件系统操作 系统无法启动时,通过救援模式启动 2.放入系统光盘,重启从光盘启动: 4.选择语言,默认English就行 5.保持默 ...