CF893C Rumor (并查集)

codeforces地址:https://codeforces.com/problemset/problem/893/C

CF893C Rumor

题目描述

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.

输入格式

The first line contains two integer numbers $ n $ and $ m $ ( $ 1<=n<=10{5},0<=m<=10 $ ) — 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}<=10^{9} $ ) — 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.

输出格式

Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.

输入输出样例 #1

输入 #1

5 2
2 5 3 4 8
1 4
4 5

输出 #1

10

输入输出样例 #2

输入 #2

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

输出 #2

55

输入输出样例 #3

输入 #3

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

输出 #3

15

说明/提示

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.

思路

不难发现是并查集的简单应用,这题注意要进行路径压缩,否则会超时

并查集模板

vector<int> father = vector<int> (n, 0);


void init() {
    for (int i = 0; i < n; ++i) {
        father[i] = i;
    }
}

int find(int u) {
    return u == father[u] ? u : find(father[u]);
}

bool isSame(int u, int v) {
   u = find(u);
   v = find(v);
   return u == v;
}
void join(int u, int v) {
    u = find(u);
    v = find(v);
    if (u == v) return ;
    father[v] = u;
}

题解

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;
int n,m;
ll c[N];
ll father[N];
void init() {
    for (int i = 1; i <= n; ++i) {
        father[i] = i;
    }
}
int find(ll u) {
    return u == father[u] ? u : father[u]=find(father[u]);#路径压缩在于father[u]=find(father[u])
}
void join(ll u, ll v) {
    u = find(u);
    v = find(v);
    if (u == v) return ;
    c[u]=min(c[u],c[v]);
    c[v]=min(c[u],c[v]);
    father[v] = u;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>c[i];
    init();
    for(int i=1;i<=m;i++)
    {
        ll a,b;
        cin>>a>>b;
        join(a,b);
    }
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        if(find(i)==i)ans+=c[i];
    }
    cout<<ans<<endl;
    return 0;
}
posted @ 2025-05-23 13:35  屈臣  阅读(34)  评论(0)    收藏  举报