Description:
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
题目大意:
n个人传消息, 其中有一些是朋友。 对于传消息的代价是对于是朋友的人他们只需要支付一个人的钱他就会告诉它的其他朋友, 对于没有朋友的人只能支付这个人的价钱。问最少要花多少钱。
解题思路:
并查集, 压缩之后看有几棵树, 对于每棵树的话支付代价最小的那个节点即可。
代码:
#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
using namespace std;
/*
*ios::sync_with_stdio(false);
*/
typedef long long ll;
typedef unsigned long long ull;
const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = (int)1e9 + 7;
const int Max = (int) 1e5 + 7;
int Fa[Max], pri[Max], cnt[Max], n, m;
void Init() {
for (int i = 1; i <= n; ++i) {
Fa[i] = i;
}
}
int Pa(int x) {
return (x == Fa[x]) ? x : Fa[x] = Pa(Fa[x]);
}
int main() {
//freopen("input.txt", "r", stdin);
scanf("%d %d", &n, &m);
Init();
for (int i = 1; i <= n; ++i) {
scanf("%d", &pri[i]);
}
int l, r;
for (int i = 1; i <= m; ++i) {
scanf("%d %d", &l, &r);
Fa[Pa(l)] = Pa(r);
}
ll ans = 0;
memset(cnt, 0, sizeof(cnt));
for (int i = 1; i <= n; ++i) {
if (Pa(i) == i) {
cnt[i] = 1e9;
}
}
for (int i = 1; i <= n; ++i) {
cnt[Pa(i)] = min(cnt[Pa(i)], pri[i]);
}
for (int i = 1; i <= n; ++i) {
if (Pa(i)== i) {
ans += cnt[i];
}
}
printf("%lld\n", ans);
return 0;
}