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 c i 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 c i (0 ≤ c i ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.
Then m lines follow, each containing a pair of numbers ( x i, y i) which represent that characters x i and y i are friends (1 ≤ x i, y i ≤ n, x i ≠ y i). 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.
我们可以使用并查集来维护在同一集合内的最少费用,最后再遍历一次集合,加上所有的最小值即可
#include<bits/stdc++.h>
#define x first
#define y second
#pragma GCC optimize(2)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize(3)
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("sse3","sse2","sse")
#pragma GCC target("avx","sse4","sse4.1","sse4.2","ssse3")
#pragma GCC target("f16c")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#pragma GCC diagnostic error "-std=c++14"
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=1e5+10;
const int M=1e6;
const int INF=0x3f3f3f3f;
const int MOD=1000000007;
int n,m;
int p[N],cost[N];
bool st[N];
int a[N];
void init(int n){
for(int i=1;i<=n;i++){
p[i]=i;
cost[i]=a[i];
st[i]=false;
}
}
int Find(int x){
if(p[x]!=x) p[x]=Find(p[x]);
return p[x];
}
int main()
{
IOS;
while(cin>>n>>m){
for(int i=1;i<=n;i++) cin>>a[i];
init(n);
for(int i=1;i<=m;i++){
int x,y;cin>>x>>y;
cost[Find(y)]=min(cost[Find(y)],cost[Find(x)]);
p[Find(x)]=Find(y);
}
LL ans=0;
for(int i=1;i<=n;i++){
if(st[Find(i)]) continue;
ans+=(LL)cost[Find(i)];
st[Find(i)]=true;
}
cout<<ans<<endl;
}
return 0;
}