CF C. Rumor
来源:互联网 发布:一起牛软件下载 编辑:程序博客网 时间:2024/06/08 17:17
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.
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.
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
5 22 5 3 4 81 44 5
10
10 01 2 3 4 5 6 7 8 9 10
55
10 51 6 2 7 3 8 4 9 5 101 23 45 67 89 10
15
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>#include<string.h>using namespace std;const int maxn=1e5+10;int c[maxn];int f[maxn];int cnt[maxn];int find(int x){return x==f[x]?x:f[x]=find(f[x]);}int main(){int n,m;while(scanf("%d%d",&n,&m)!=EOF){for(int i=1;i<=n;i++)f[i]=i;for(int i=1;i<=n;i++){scanf("%d",&c[i]);} int a,b;for(int i=0;i<m;i++){scanf("%d %d",&a,&b);int aa=find(a);int bb=find(b);f[aa]=bb; } long long ans=0; memset(cnt,0,sizeof(cnt)); for(int i=1;i<=n;i++) { if(find(i)==i) cnt[i]=1e9; } for(int i=1;i<=n;i++) { cnt[find(i)]=min(cnt[find(i)],c[i]); } for(int i=1;i<=n;i++) { if(find(i)==i) ans+=cnt[i]; } cout<<ans<<endl;}return 0;}
- CF C. Rumor
- CodeForces 893C Rumor
- codeforces 893 C.Rumor
- Codeforce 893C Rumor (dfs)
- Educational Codeforces Round 33 C. Rumor
- 【Educational Codeforces Round 33】 C. Rumor (并查集)
- A rumor spreads
- Rumor谣传路由协议
- CF 190C STL
- CF 79C. Beaver
- CF 229C Triangles
- CF 4 C
- cf 230c
- CF 255C
- CF 27C
- CF 52C
- CF 190C
- CF 191 div2 C
- angular2拦截器服务
- 浅谈消息队列
- 黑客角逐:刷脸解锁,你的iPhone X并不安全
- SQLite数据库简介
- Arduino构建与解析Json字符串实例
- CF C. Rumor
- 强推 | Google&微软数据科学家们给你讲Keras实战
- 微信小程序电商实战-首页(上)
- Item 8:别让异常逃离析构函数【effective C++读书笔记】
- 线性表的链式存储结构
- QtableWidget单元格设置和操作
- Java异常
- kubernetes Pull Image 鉴权过程
- dubbo+zookeeper安装配置