Educational Codeforces Round 33 C. Rumor

来源:互联网 发布:淘宝联盟显示已收货 编辑:程序博客网 时间:2024/05/23 16:15

原题:
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 ≤ 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.

中文:
有一个人要去一个城市里散布谣言,这个城市里的人有的互相认识,如果互相人的人力其中有一个人知道,那么这个圈里的人就都知道。每个人听信谣言的成本是ai,现在给你n个人,和这些人相互认识的关系。问你最少花多少成本就能让所有人都知道谣言。

#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn=100001;int n,m;int father[maxn];ll Rank[maxn];int Find(int x){    if(father[x]==x)        return father[x];    else        return father[x]=Find(father[x]);}void Union(int a,int b){    int x=Find(a);    int y=Find(b);    if(Rank[x]>Rank[y])        father[x]=y;    else        father[y]=x;}int main(){    ios::sync_with_stdio(false);    while(cin>>n>>m)    {        memset(Rank,0,sizeof(Rank));        for(int i=1;i<=n;i++)        {            father[i]=i;            cin>>Rank[i];        }        int x,y;        for(int i=1;i<=m;i++)        {            cin>>x>>y;            Union(x,y);        }        for(int i=1;i<=n;i++)            father[x]=Find(father[x]);        ll ans=0;        for(int i=1;i<=n;i++)            if(father[i]==i)                ans+=Rank[i];        cout<<ans<<endl;    }    return 0;}

解答:

实际上就是无向图找出所有连通分量,然后把每个连通分量里ai值最小的找出来加一起就是最后结果。
可以使用带秩的并查集,在一个圈里的人连到一个根节点上,这个根节点每次更新为ai最小的那个值。
最后把所有根节点的值加一起就可以。

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 seiko是什么牌子 selko是什么牌子手表 seiko手表型号查询 selko是什么牌子手表价格 seiko手表 seiko手表价格查询 seiko手表价格 seiko 精巧近义词 精巧反义词 精巧 精巧的意思 精巧的拼音 精巧拼音 精巧绝伦 精巧的造句 精巧的近义词 精巧的反义词 精巧的意思是什么 白鹭是一首精巧的诗仿写句子 精巧的反义词是什么 精巧什么意思 精巧的拼音怎么写 精巧的近义词和反义词 精巧型压力变送器 精巧的拼音是什么 精巧的反义词是什么呢 为什么说白鹭是一首精巧的诗 精帝 逆梦寒 精干 精干短发 黑熊精干观音菩萨h 精干是什么意思 酒精度 单精度双精度 精度 啤酒精度 精度等级 压力表精度 酒精度vol 双精度