前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >codeforce893C (并查集)

codeforce893C (并查集)

作者头像
dejavu1zz
发布2020-10-23 15:19:13
4880
发布2020-10-23 15:19:13
举报
文章被收录于专栏:奇妙的算法世界

题意描述

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.

思路

我们可以使用并查集来维护在同一集合内的最少费用,最后再遍历一次集合,加上所有的最小值即可

AC代码

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/05/08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意描述
  • 思路
  • AC代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档