CodeForces - 893C Rumor(并查集)

C. Rumor
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 ≤ 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.


题目大意是:找到向所有人传播流言的最低成本,第二行,是n个人分别传播的成本,3到m+2行是哪些人会互相传播。

思路:

开2个数组分别表示  价值v  和  父亲f  

每次找到a,b的根节点fa,fb,然后合并  f[fa]=fb;  使根节点的v[fb]的价值v[fa]和v[fb]中取较小值。

如此反复每个根节点存的都是该树所需最小成本。(详见代码,我得滚回小学学语文_(:зゝ∠)_

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<queue>
using namespace std;
#define eps 1e-8
#define mod 1000000007
#define ll long long
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define maxn 100005

int f[maxn],v[maxn];

//找到根节点
int getf(int n){
    if(f[n]==n)return n;
    f[n]=getf(f[n]);
    return f[n];
}

int main(){
    int n,m,i,a,b,fa,fb;
    scanf("%d%d",&n,&m);
    
    //初始化
    for(i=1;i<=n;i++){
        scanf("%d",&v[i]);
        f[i]=i;
    }
    
    while(m--){
        scanf("%d%d",&a,&b);
        fa=getf(a);
        fb=getf(b);
        f[fa]=fb;
        if(v[fb]>v[fa])v[fb]=v[fa]; //刷新根节点价值
    }
    
    ll ans=0;       //ans要用64位长整数存不然会爆
    for(i=1;i<=n;i++){
        if(f[i]==i)ans+=v[i];   //加上每个根节点得价值
    }
    printf("%lld\n",ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值