893CRumor
来源:互联网 发布:淘宝分店怎么开 编辑:程序博客网 时间:2024/06/05 10:54
并查集
用并查集模板,把属于同一个集合归在一起,然后集合中取最小的集合,也就是花费黄金最少的集合。
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 arencharacters 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 wantscigold 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 allncharacters 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 numbersnandm(1 ≤n≤ 105, 0 ≤m≤ 105) — the number of characters in Overcity and the number of pairs of friends.
The second line containsninteger numbersci(0 ≤ci≤ 109) — the amount of goldi-th character asks to start spreading the rumor.
Thenmlines follow, each containing a pair of numbers (xi,yi) which represent that charactersxiandyiare 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.
#include<cstdio>#include<iostream>#include<algorithm>using namespace std;const int N=1e5+10;int m,n;long long ans;int fa[N],a[N],x,y;int find(int x){ if(x!=fa[x]) fa[x]=find(fa[x]); return fa[x];}void meger(int x,int y){ int xx=find(x); int yy=find(y); if(xx!=yy) { fa[xx]=yy; a[yy]=min(a[yy],a[xx]); }}int main(){ while(cin>>n>>m) { ans=0; for(int i=1; i<=n; i++) fa[i]=i; for(int i=1; i<=n; i++) cin>>a[i]; for(int i=0; i<m; i++) { cin>>x>>y; meger(x,y); } for(int i=1;i<=n;i++) if(i==find(i)) ans+=a[i]; cout<<ans<<endl; } return 0;}
- 893CRumor
- Codeforces Educational Codeforces Round 33 C 893CRumor(并查集)
- UVa 893 - Y3K Problem
- Codeforces 893E
- 893BBeautiful Divisors
- 893AChess For Three
- CodeForces 893C Rumor
- Codeforces 893E
- Codeforces 893E 数论
- codeforces 893D
- codeforces 893 C.Rumor
- CF 893C DFS
- CodeForces 893B Beautiful Divisors
- codeforce 893B (模拟)
- Codeforce 893C Rumor (dfs)
- codeforces 893B. Buggy Robot
- CodeForces 893B Beautiful Divisors
- UVa 893 Y3K Problem (用GregorianCalendar类秒杀)
- 代码大全第二部分 (包含第四章)
- 欢迎使用CSDN-markdown编辑器
- printf函数的实现
- [MVVM]02-MVVMLight页面前后台消息和命令传递
- solve the version conflict of google test on ubuntu
- 893CRumor
- InnoDB的行锁
- spring boot No Identifier specified for entity的解决办法
- 【百度、腾讯、阿里等】+【JAVA开发实习生】+春招面试经验
- C与指针 7-9章
- Java将字符串转换为时间,支持多种格式
- Android网络框架
- 简述ByteBuffer的使用
- Centos7下将FTP用户锁定在指定目录的解决办法