日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Rumor CodeForces - 893C(并查集)

發布時間:2023/12/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Rumor CodeForces - 893C(并查集) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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
5 2
2 5 3 4 8
1 4
4 5
Output
10
Input
10 0
1 2 3 4 5 6 7 8 9 10
Output
55
Input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
Output
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.
思路:算是一個并查集的簡單變形。只不過我們需要把傳播謠言花費最小的作為祖先。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=1e5+100; struct node{int x,y; }p[maxx]; int a[maxx]; int f[maxx]; int n,m;int getf(int u) {return u==f[u]?u:f[u]=getf(f[u]); } void merge(int x,int y) {int t1=getf(x);int t2=getf(y);if(t1!=t2){if(a[t1]>a[t2]) f[t1]=t2;else f[t2]=t1;} } int main() {while(~scanf("%d%d",&n,&m)){for(int i=1;i<=n;i++) scanf("%d",&a[i]);for(int i=1;i<=m;i++) scanf("%d%d",&p[i].x,&p[i].y);for(int i=1;i<=n;i++) f[i]=i;for(int i=1;i<=m;i++) merge(p[i].x,p[i].y);ll ans=0;for(int i=1;i<=n;i++) if(f[i]==i) ans+=(ll)a[i];cout<<ans<<endl;}return 0; }

努力加油a啊

總結

以上是生活随笔為你收集整理的Rumor CodeForces - 893C(并查集)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。