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 ≤ nxi ≠ 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<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; int pre[100005]; int find(int x)
{
if(x==pre[x])
{
return x;
}
else
{
return pre[x]=find(pre[x]);
}
}
int a[100005];
bool merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
if(a[fx]>=a[fy])
pre[fx]=fy;
else
{
pre[fy]=fx;
} return true;
}
else
{
return false;
}
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
pre[i]=i;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int x,y;
long long int sum=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(merge(x,y)==true)
{
merge(x,y);
}
} for(int i=1;i<=n;i++)
{
if(find(i)==i)
{
sum+=a[i];
}
} cout<<sum<<endl; return 0;
}

CodeForces - 893C-Rumor(并查集变式)的更多相关文章

  1. 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 ...

  2. CodeForces 893C (并查集板子题)

    刷题刷到自闭,写个博客放松一下 题意:n个人,m对朋友,每寻找一个人传播消息需要花费相应的价钱,朋友之间传播消息不需要花钱,问最小的花费 把是朋友的归到一起,求朋友中花钱最少的,将所有最少的加起来. ...

  3. CodeForces - 893C Rumor【并查集】

    <题目链接> 题目大意: 有n个人,其中有m对朋友,现在你有一个秘密你想告诉所有人,第i个人愿意出价a[i]买你的秘密,获得秘密的人会免费告诉它的所有朋友(他朋友的朋友也会免费知道),现在 ...

  4. Codeforces 731C Socks 并查集

    题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...

  5. codeforces 722C (并查集)

    题目链接:http://codeforces.com/contest/722/problem/C 题意:每次破坏一个数,求每次操作后的最大连续子串和. 思路:并查集逆向操作 #include<b ...

  6. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 828C) - 链表 - 并查集

    Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...

  7. Codeforces 455C Civilization(并查集+dfs)

    题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...

  8. codeforces 893C Rumor 前向星+dfs

    893C Rumor 思路: 前向星+DFS 代码: #include <bits/stdc++.h> using namespace std; #define _for(i,a,b) f ...

  9. Mobile Phone Network CodeForces - 1023F(并查集lca+修改环)

    题意: 就是有几个点,你掌控了几条路,你的商业对手也掌控了几条路,然后你想让游客都把你的所有路都走完,那么你就有钱了,但你又想挣的钱最多,真是的过分..哈哈 游客肯定要对比一下你的对手的路 看看那个便 ...

随机推荐

  1. ffmpeg部分编译选项

    -enable-neon  (如果使用了arm公司提供的neon加速技术,则可以并行的读取多个元素.参考网址:http://www.cnblogs.com/hrlnw/p/3723072.html) ...

  2. C++下基本类型所占位数和取值范围

    原文:http://hi.baidu.com/magicdemon/blog/item/821b2e22d7df494cad34debd.html C++下基本类型所占位数和取值范围: 符号属性    ...

  3. Boost中实现线程安全

    博客转载自: http://www.cnblogs.com/lvdongjie/p/4447142.html 1 boost原子变量和线程 #include <boost/thread.hpp& ...

  4. Qt嵌入式开发环境搭建

    一.Qt版本介绍 按照不同的图形界面来划分,分为四个版本: 1.Win32版:适用于windows平台 2.X11版:适用于各种X系统的Linux和Unix平台 3.Mac版:适用于苹果的MacOS ...

  5. 算法Sedgewick第四版-第1章基础-025-用队列实现unix下的Directory命令

    package algorithms.util; /************************************************************************** ...

  6. DIY的RPM包怎么签名呢 - 笔记

    参考 https://gist.github.com/fernandoaleman/1376720 如果打不开上一个连接,请参考https://www.cnblogs.com/LiuYanYGZ/p/ ...

  7. 数据结构_yjjsj(伊姐姐数字游戏)

    问题描述 伊姐姐热衷于各类数字游戏, 24 点. 2048.数独等轻轻松松毫无压力.一日,可爱的小姐姐邀请伊姐姐一起玩一种简单的数字 game,游戏规则如下:一开始桌上放着 n 张数字卡片,从左到右按 ...

  8. DBUtils工具类和DBCP连接池

    今日内容介绍 1.DBUtils2.处理结果集的八种方式3.连接池4.连接池的用法1 PrepareStatement接口预编译SQL语句 1.1 预处理对象 * 使用PreparedStatemen ...

  9. python运算优先级

    运算符优先级(下面的优先级高) 运算符  描述 lambda  Lambda表达式 or  布尔“或”  and  布尔“与” not x   布尔“非”  in not in 成员测试 is    ...

  10. android 设置颜色的三种方法

    1.利于系统自带的颜色类 如TextView1.setTextColor(Android.graphics.Color.RED); 2.数字颜色表示法 TextView1.setTextColor(0 ...