C. Rumor
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

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 ≤ nxi ≠ 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.

【题意】:有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【并查集+贪心/维护集合最小值】的更多相关文章

  1. Educational Codeforces Round 78 (Rated for Div. 2)D(并查集+SET)

    连边的点用并查集检查是否有环,如果他们的fa是同一个点说明绕了一圈绕回去了.n个点一共能连n-1条边,如果小于n-1条边说明存在多个联通块. #define HAVE_STRUCT_TIMESPEC ...

  2. Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays

    题目链接 题意:给你两个数x,yx,yx,y,让你构造一些长为yyy的数列,让这个数列的累乘为xxx,输出方案数. 思路:考虑对xxx进行质因数分解,设某个质因子PiP_iPi​的的幂为kkk,则这个 ...

  3. Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)

    题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...

  4. Educational Codeforces Round 33 (Rated for Div. 2) 题解

    A.每个状态只有一种后续转移,判断每次转移是否都合法即可. #include <iostream> #include <cstdio> using namespace std; ...

  5. Educational Codeforces Round 33 (Rated for Div. 2)A-F

    总的来说这套题还是很不错的,让我对主席树有了更深的了解 A:水题,模拟即可 #include<bits/stdc++.h> #define fi first #define se seco ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. install ironic-inspector

    安装相应的包和组件 yum install openstack-ironic-inspector python-ironic-inspector-client -y 创建user openstack ...

  2. 第二阶段团队冲刺-six

    昨天: 完成打印名单的功能. 今天: 合并程序(添加打印txt). 遇到的问题: web.xml中配置url-pattern一直不合适,不知道为什么会影响界面.

  3. 第九章 广播和本地组播(IGMP和MLD)

    距离项目开启已经过去了一段时间,这段时间内自己学习的内容也算挺多的,但是也较容易遗忘,之后应该在空余的时间内多翻翻博客,更加清楚传统计算机网络的运作. 由于51要出去玩,更要好好利用好最近的时间.完成 ...

  4. js文字跳动效果

    /*! * chaffle v1.0.0 * * Licensed under MIT * Copyright 2013-2014 blivesta * http://blivesta.com */ ...

  5. BZOJ 2438:杀人游戏(tarjan+概率)

    杀人游戏Description一位冷血的杀手潜入 Na-wiat,并假装成平民.警察希望能在 N 个人里面,查出谁是杀手. 警察能够对每一个人进行查证,假如查证的对象是平民,他会告诉警察,他认识的人, ...

  6. 点对点协议(Point-to-Point Protocol)

    简介 点对点协议简称PPP协议,工作在数据链路层.设计目的主要是用来通过拨号或专线方式建立点对点连接发送数据,使其成为各种主机. 网桥和路由器之间简单连接的一种共通的解决方案. PPP协议的组成 建立 ...

  7. [CF932D]Tree

    题目大意:两种操作: $1\;u\;w:$把下一个点挂在$u$下,权值为$w$. $2\;u\;w:$询问从$u$开始的序列的最长长度.序列为从$u$开始的祖先序列中的不严格上升序列 题解:可以把一个 ...

  8. Hash表模板

    namespace Hash { ; ; struct adj { ll nxt,v,num,val; }e[N]; ll head[H],ecnt=; void init() { ecnt=; me ...

  9. poj1679 次最小生成树 kruskal(暴力枚举)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

  10. Agile已死, Agility长存

    注:本文系作者独立翻译,可以随意转载.如有雷同,纯属巧合.原文地址:http://pragdave.me/blog/2014/03/04/time-to-kill-agile/ P.s. 第一次自己翻 ...