CodeForces - 893C Rumor 并查集

链接http://codeforces.com/problemset/problem/893/C

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 ≤ n, xi ≠ 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

Copy

5 2
2 5 3 4 8
1 4
4 5

Output

Copy

10

Input

Copy

10 0
1 2 3 4 5 6 7 8 9 10

Output

Copy

55

Input

Copy

10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10

Output

Copy

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.


保证每一个团体中贿赂的钱都是其中最便宜的那个

注意数据范围和long long

//---JQM---//
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define maxn 100050
//int fa[maxn];
ll ans;

struct stu
{
	int fa, val;
}fa[maxn];

int find(int x)
{
	if(fa[x].fa==x)
		return x;
	return fa[x].fa = find(fa[x].fa);
}

int join(int x, int y)
{
	int f1 = find(x);
	int f2 = find(y);
	if(f1!=f2)
	{
		fa[f1].fa = f2;
		fa[f2].val = min(fa[f1].val, fa[f2].val);
		fa[f1].val = 0;
		return 1;
	}
	return 0;
}

int main()
{
	int n, m, i, x, y;
	cin>>n>>m;
	for(i=1; i<=n; i++)
	{
		fa[i].fa = i;
		scanf("%d", &fa[i].val);
	}
	ans = 0;
	for(i=1; i<=m; i++)
	{
		scanf("%d%d", &x, &y);
		join(x, y);
	}
	for(i=1; i<=n; i++)
	{
		ans += fa[i].val;
	}
	cout<<ans<<endl;

	return 0;
}

猜你喜欢

转载自blog.csdn.net/Jqmjyj/article/details/82019888
今日推荐