CodeForces - 893C Rumor (并查集)

原题地址:http://codeforces.com/problemset/problem/893/C


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 y
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 个人和 m组关系 

告诉 编号从 1 ~ n 的人 传播谣言所需的金币数 

以及m组关系所对应的两人的编号

求出 最少需要多少金币 使得所有人都知道谣言

看到题目和样例  马上想到是并查集 但是 个人赛的时候并没有做出来(*  ̄︿ ̄)


参考 荆门王的博客:https://blog.csdn.net/qq_36258516/article/details/78787188

套用原先并查集的模板做的

放代码

#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define M 100005
int father[M];
long long mm[M];
int ff(int x)//获取父节点
{
    if (x!=father[x])
        father[x]=ff(father[x]);//路径压缩 修改的是father数组
    return father[x];
}
void join(int a,int b)//合并两个元素所在的集合
{
    int x,y;
    x=ff(a);
    y=ff(b);
    if (x!=y)
    {
        father[x]=y;
    }
}
int main()
{
    int t,n,m,a,b;
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)//初始化并查集
        father[i]=i;
    for(int i=1; i<=n; i++)
        scanf("%lld",&mm[i]);
    for (int i=1; i<=m; i++)
    {
        scanf("%d%d",&a,&b);
        join(a,b);
    }
    for(int i=1; i<=n; i++)
        mm[ff(i)]=min(mm[ff(i)],mm[i]);//记录同一并查集中最小的权值
    long long sum=0;
    for(int i=1; i<=n; i++)
        if(i==ff(i))
            sum+=mm[i];
    printf("%lld\n",sum);
    return 0;
}



猜你喜欢

转载自blog.csdn.net/oneline_/article/details/80757057
今日推荐