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.

Examples
Input
5 2
2 5 3 4 8
1 4
4 5
Output
10
Input
10 0
1 2 3 4 5 6 7 8 9 10
Output
55
Input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
Output
15
Note
In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.

In the second example Vova has to bribe everyone.

In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters.
思路:算是一个并查集的简单变形。只不过我们需要把传播谣言花费最小的作为祖先。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=1e5+100;
struct node{int x,y;
}p[maxx];
int a[maxx];
int f[maxx];
int n,m;int getf(int u)
{return u==f[u]?u:f[u]=getf(f[u]);
}
void merge(int x,int y)
{int t1=getf(x);int t2=getf(y);if(t1!=t2){if(a[t1]>a[t2]) f[t1]=t2;else f[t2]=t1;}
}
int main()
{while(~scanf("%d%d",&n,&m)){for(int i=1;i<=n;i++) scanf("%d",&a[i]);for(int i=1;i<=m;i++) scanf("%d%d",&p[i].x,&p[i].y);for(int i=1;i<=n;i++) f[i]=i;for(int i=1;i<=m;i++) merge(p[i].x,p[i].y);ll ans=0;for(int i=1;i<=n;i++) if(f[i]==i) ans+=(ll)a[i];cout<<ans<<endl;}return 0;
}

努力加油a啊

Rumor CodeForces - 893C(并查集)相关推荐

  1. C - Rumor CodeForces - 893C

    目录 题目链接 一些话 流程 套路 ac代码 题目链接 C - Rumor CodeForces - 893C 一些话 拆解流程的时候没想到res 开long long,但写的时候想到了,一开始还庆幸 ...

  2. A - Cthulhu CodeForces - 103B (并查集)

    -Once upon a time a man came to the sea. The sea was stormy and dark. The man started to call for th ...

  3. Rumor CodeForces - 893C(并查集基本操作:维护连通块最值)

    题目链接 PS:这题也可以用dfs搜连通块,一遍搜一遍记录连通块的最值. AC代码: #include <iostream> #include <cstring> #inclu ...

  4. Rumor CodeForces - 893C

    Vova promised himself that he would never play computer games... But recently Firestorm - a well-kno ...

  5. Educational Codeforces Round 33 (Rated for Div. 2)C.Rumor并查集

    Educational Codeforces Round 33 (Rated for Div. 2)C.Rumor并查集 题意:首先用并查集把N个人分成几块,然后每个块当中选取一个最小权值 加到答案中 ...

  6. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction 并查集

    C. String Reconstruction 题目连接: http://codeforces.com/contest/828/problem/C Description Ivan had stri ...

  7. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  8. Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集

    http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...

  9. Secret Passwords CodeForces - 1263D(并查集)

    One unknown hacker wants to get the admin's password of AtForces testing system, to get problems fro ...

最新文章

  1. java使用jdom生成xml格式文件
  2. 读书笔记《鸟哥的Linux私房菜-基础学习篇》man page 查询数据后数字的意思
  3. 时间序列预测方法汇总:从理论到实践(附Kaggle经典比赛方案)
  4. Web网站架构设计(转)
  5. Kenshin Cui's Blog
  6. 首届大湾区 DevOps/微服务秋季分享会圆满落幕!
  7. 面试必备:CAS无锁机制
  8. 安装php 端口被占用,安装phpstudy,APMServ启动服务提示80端口被占用
  9. 备忘: MIRACL 大数运算库使用手册
  10. 基于非特定人语音识别芯片的技术方案
  11. Matlab矩阵各行各列求和的不同方法
  12. 初识SaltStack
  13. k8s Service
  14. 【车间调度】基于matlab遗传算法求解多目标流水车间调度问题【含Matlab源码 443期】
  15. java snap7_Snap7 referance manual PDF 下载
  16. 自己建网站的步骤及方法
  17. IDEA补丁破解使用方法
  18. 官网USB读卡器移植(TF卡SDIO模式 + SPI-flash)
  19. 网工学习 第十一章 网络管理 网络管理基础 网络管理协议五大标准 SNMP协议 RMON
  20. Matlab中nargin,nargout,varargout以及varargin的用法 -

热门文章

  1. 机器人出魔切还是三相_哇!电站凝汽器清洗居然可以用机器人啦
  2. 3、AD使用技巧分享
  3. oracle 11g 精简,Oracle 11g 精简客户端
  4. 显示unc路径服务器根目录,错误:“您必须输入带有盘符的完整路径,例如:C:\ APPor形式的UNC路径:\\服务器\共享”...
  5. 线程池参数详解_java中常见的六种线程池详解
  6. 风格迁移应用_图像也能做情感迁移?罗切斯特大学团队提出计算机视觉新任务...
  7. Android开发之在任意Activity在广点通页面添加自定义布局在穿山甲页面添加任意布局
  8. Android studio删除sdk的方法(图文教程)
  9. Android 百度地图之全局搜索周边搜索全国搜索城市路线规划(升级版附源码)
  10. 计算机vb基础知识,2018年计算机二级考试VB基础知识:参数的传递