Educational Codeforces Round 33 (Rated for Div. 2) C. Rumor【并查集+贪心/维护集合最小值】
2 seconds
256 megabytes
standard input
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.
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 2
2 5 3 4 8
1 4
4 5
10
10 0
1 2 3 4 5 6 7 8 9 10
55
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 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.
【题意】:有n个人,其中有m对朋友,现在你有一个秘密想告诉所有人,第i个人愿意出价a[i]买你的秘密,获得秘密的人会免费告诉他所有的朋友(他朋友的朋友的···也会免费知道),现在他们想出最少的价钱买秘密,那么你最少能得到多少钱?
【分析】:最后会形成多个集合,每个集合里面的人能够可以互相到达。维护并查集的时候,顺便维护一下每个集合里面的最小值。最后答案就为∑min{每个集合}。
【题解】:
#include <bits/stdc++.h> using namespace std;
typedef long long ll;
const int N = ;
ll n,m;
ll x,y;
ll a[N],fa[N];
void init()
{
for(ll i=;i<=n;i++)
fa[i]=i;
}
ll Find(ll x)
{
return fa[x]==x?x:x=Find(fa[x]);
} void Join(ll x,ll y)
{
ll fx = Find(x);
ll fy = Find(y);
if(fx!=fy){
fa[fx]=fy;
a[fy]=min(a[fx],a[fy]); //维护一下每个集合里面的最小值
}
/* or
for(int i = 1; i <= n; ++i){
a[fa(i)] = min(a[fa(i)], a[i]);
}
*/
}
int main()
{
ll sum=;//ll防炸 or wa7
scanf("%lld %lld",&n,&m);
init();
for(ll i=;i<=n;i++) scanf("%lld",&a[i]); for(ll i=;i<=m;i++) {
scanf("%lld%lld",&x,&y);
Join(x,y);
}
for(ll i=;i<=n;i++){
if( fa[i]==i )
sum+=a[i];
}
printf("%lld\n",sum);
return ;
}
并查集
Educational Codeforces Round 33 (Rated for Div. 2) C. Rumor【并查集+贪心/维护集合最小值】的更多相关文章
- Educational Codeforces Round 78 (Rated for Div. 2)D(并查集+SET)
连边的点用并查集检查是否有环,如果他们的fa是同一个点说明绕了一圈绕回去了.n个点一共能连n-1条边,如果小于n-1条边说明存在多个联通块. #define HAVE_STRUCT_TIMESPEC ...
- Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays
题目链接 题意:给你两个数x,yx,yx,y,让你构造一些长为yyy的数列,让这个数列的累乘为xxx,输出方案数. 思路:考虑对xxx进行质因数分解,设某个质因子PiP_iPi的的幂为kkk,则这个 ...
- Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)
题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...
- Educational Codeforces Round 33 (Rated for Div. 2) 题解
A.每个状态只有一种后续转移,判断每次转移是否都合法即可. #include <iostream> #include <cstdio> using namespace std; ...
- Educational Codeforces Round 33 (Rated for Div. 2)A-F
总的来说这套题还是很不错的,让我对主席树有了更深的了解 A:水题,模拟即可 #include<bits/stdc++.h> #define fi first #define se seco ...
- Educational Codeforces Round 33 (Rated for Div. 2) D. Credit Card
D. Credit Card time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Educational Codeforces Round 33 (Rated for Div. 2) B. Beautiful Divisors【进制思维/打表】
B. Beautiful Divisors time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Educational Codeforces Round 33 (Rated for Div. 2) A. Chess For Three【模拟/逻辑推理】
A. Chess For Three time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Educational Codeforces Round 33 (Rated for Div. 2)
A. Chess For Three time limit per test 1 second memory limit per test 256 megabytes input standard i ...
随机推荐
- LDAP操作的两种方案
最近由于项目需要研究了一下LDAP相关知识,感觉对没接触过的人来说还是有点坑的,所以记录下来给大家分享. 由于是第一次接触,就在网上搜了一些相关的文章,照着示例代码测试,却怎么也连不上LDAP服务器, ...
- USACO Section1.2 Dual Palindromes 解题报告
dualpal解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...
- leetcode 【 Merge Two Sorted Lists 】 python 实现
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- jeakins用户配置
进入jeakins:系统管理-全局安全设置 如果有多个用户视情况而定进行权限配置
- springboot整合jersey
https://blog.csdn.net/xiongpei00/article/details/76576420
- 对于进程没杀死占用内存和cpu行为的方法
在跑机器学习或者深度学习的过程中有可能遇到没杀死进程的情况,但是程序的入口又没关掉,尤其是我使用jupyter从远程Linux映射到windows浏览器跑程序的时候 对于上面的问题, 首先运行 hto ...
- 条件随机场(Conditional random field)
条件随机场真是把我给折磨坏了啊,本以为一本小小的<统计学习方法>攻坚剩下最后一章,心情还是十分愉悦的,打算一口气把它看完,结果真正啃起来真是无比的艰难啊,每一句对我都好像是天书一般,怎么这 ...
- [译]如何禁止Requests库的log日志信息呢?
原文来源: https://stackoverflow.com/questions/11029717/how-do-i-disable-log-messages-from-the-requests-l ...
- NetScaler的cookieinsert和sourceip联合保持机制
NetScaler的cookieinsert和sourceip联合保持机制 使用NetScaler的cookieinsert和sourceip联合进行session保持机制即主用cookieinser ...
- 【转】oracle 删除重复记录
转至:http://blog.163.com/aner_rui/blog/static/12131232820105901451809/ 2.保留一条(这个应该是大多数人所需要的 ^_^) Delet ...