您的位置:首页 > 其它

Codeforces Educational Codeforces Round 33 C 893C Rumor(并查集)

2017-11-24 22:57 549 查看
C. Rumor

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

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 ≤ 10^5, 0 ≤ m ≤ 10^5) — 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

题意:有流言要在人群中传播,并且,传播需要花费一定的金钱,但是,如果互为朋友,就不用花钱,而且,朋友的朋友也是朋友,问最少需要多少钱才能让每个人都知道这个流言

思路:非常明显的并查集,在并查集查找的时候,顺便维护一个团体所贡献的最小值,最后加起来。

如果不懂并查集的,我推荐去看看这位dalao的博客,我之前也是从这里学的

并查集入门

#include<bits/stdc++.h>
#include<iostream>
using namespace std;

long long C[100005] ;
int  pre[100005];
bool t[100005];               //t 用于标记独立块的根结点

int Find(int x)
{
int Min = C[x] ;
int r=x;
while(r!=pre[r])
{
C[r] = min(C[r] , C[pre[r]]) ;
C[pre[r]] = min(C[r] , C[pre[r]]) ;  //维护最小值
r=pre[r];
}
int i=x,j;
while(pre[i]!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}

void mix(int x,int y)
{
int fx=Find(x),fy=Find(y);
if(fx!=fy)
{
pre[fy]=fx;
}
}

int main()
{
int N,M,a,b,i,j;
long long ans = 0 ;
scanf("%d%d",&N,&M) ;
for(int i = 1 ; i <= N ; i ++)
{
scanf("%d", &C[i]) ;
}
for(i=1; i<=N; i++)        //初始化
{
pre[i]=i;
}
for(i=1; i<=M ; i++)        //吸收并整理数据
{
scanf("%d%d",&a,&b);
mix(a,b);
}
memset(t,0,sizeof(t));
for(i=1; i<=N; i++)        //标记根结点
{
t[Find(i)]=1;
}
for(int i = 1 ; i <= N ; i ++)
{
if(t[i])
{
ans = ans + C[i] ;
}
}
cout << ans << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces