【Educational Codeforces Round 33】 C. Rumor (并查集)
来源:互联网 发布:thinkphp5 网站源码 编辑:程序博客网 时间:2024/05/23 18:08
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 <bits/stdc++.h>using namespace std;int p[100005], a[100005], cnt[100005];int findset(int x){return p[x] == x ? x : p[x] = findset(p[x]);}int main(){int n, m;cin >> n >> m;for(int i = 1; i <= n; ++i){p[i] = i;}for(int i = 1; i <= n; ++i){scanf("%d", &a[i]);}int x, y;for(int i = 1; i <= m; ++i){scanf("%d %d", &x, &y);p[findset(x)] = findset(y);}long long ans = 0;memset(cnt, 0, sizeof(cnt));for(int i = 1; i <= n; ++i){if(findset(i) == i){cnt[i] = 1e9;}}for(int i = 1; i <= n; ++i){cnt[findset(i)] = min(cnt[findset(i)], a[i]);}for(int i = 1; i <= n; ++i){if(findset(i) == i){ans += cnt[i];}}cout<<ans<<endl;}/*题意:每个字母传递一个人有一个代价,朋友之间传递不需要代价,问所有人都被传到需要多少代价。思路:并查集处理朋友关系,对于一个集合只需要传递给大家最小的那个人,然后让他传递给他的所有朋友,把所有集合的最小值加起来即可。*/
- 【Educational Codeforces Round 33】 C. Rumor (并查集)
- Educational Codeforces Round 33 C. Rumor
- Codeforces Educational Codeforces Round 33 C 893CRumor(并查集)
- Educational Codeforces Round 5 C.The Labyrinth(dfs+并查集)
- Educational Codeforces Round 7 C. Not Equal on a Segment 并查集
- Educational Codeforces Round 5 C. The Labyrinth BFS,并查集
- Educational Codeforces Round 14 D 并查集
- Educational Codeforces Round 31 E. Binary Matrix 并查集
- Educational Codeforces Round 1 D. Igor In the Museum BFS,带权并查集
- CodeForces 893C Rumor
- codeforces 893 C.Rumor
- Educational Codeforces Round 33
- Educational Codeforces Round 21 C
- Educational Codeforces Round 5 C
- Educational Codeforces Round 26 C
- Codeforces Round #376 (Div. 2) C 并查集
- Codeforces Round #376 (Div. 2) C dfs+并查集
- codeforces Educational Codeforces Round 16-C
- windows驱动读写文件不成功
- 查看JVM内存信息的方法
- JVM 运行时数据区
- java实现远程桌面
- Error: ERR: stack=java.lang.StackOverflowError: stack size 8MB
- 【Educational Codeforces Round 33】 C. Rumor (并查集)
- 【重大更新】DevExpress v17.2新版亮点—WinForms篇(二)
- 使用vultr搭建 ss 教程,亲测可用
- windows下安装 ubuntu双系统教程
- 库函数strstr的实现,比较库函数strstr和strchr。
- 文章标题
- 理解MySQL——索引与优化
- Ubuntu下deb包的安装方法
- layui单独弹出iframe批量调用