Codeforces 893C Rumor (并查集)
2018-03-14 10:20
155 查看
C. Rumortime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVova 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.InputThe 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.OutputPrint one number — the minimum amount of gold Vova has to spend in order to finish the quest.ExamplesinputCopy
#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#define INF 0x3f3f3f3f
#define MOD 1e9+7
using namespace std;
int cost[100005];
int pre[100005];
int find(int x)
{
if(pre[x]==x)
return x;
return pre[x]=find(pre[x]);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&cost[i]);
pre[i]=i;
}
for(int i=0;i<m;i++)
{
int a,b,x,y;
scanf("%d %d",&x,&y);
a=find(x);
b=find(y);
if(a!=b)
{
if(cost[a]<cost[b])
pre[b]=a;
else
pre[a]=b;
}
}
long long ans=0;
for(int i=1;i<=n;i++)
if(pre[i]==i)
ans+=cost[i];
printf("%lld\n",ans);
return 0;
}
5 2 2 5 3 4 8 1 4 4 5output
10inputCopy
10 0output
1 2 3 4 5 6 7 8 9 10
55inputCopy
10 5output
1 6 2 7 3 8 4 9 5 101 2
3 4
5 6
7 8
9 10
15NoteIn 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 <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#define INF 0x3f3f3f3f
#define MOD 1e9+7
using namespace std;
int cost[100005];
int pre[100005];
int find(int x)
{
if(pre[x]==x)
return x;
return pre[x]=find(pre[x]);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&cost[i]);
pre[i]=i;
}
for(int i=0;i<m;i++)
{
int a,b,x,y;
scanf("%d %d",&x,&y);
a=find(x);
b=find(y);
if(a!=b)
{
if(cost[a]<cost[b])
pre[b]=a;
else
pre[a]=b;
}
}
long long ans=0;
for(int i=1;i<=n;i++)
if(pre[i]==i)
ans+=cost[i];
printf("%lld\n",ans);
return 0;
}
相关文章推荐
- CodeForces - 893C Rumor(并查集)
- CodeForces - 893C Rumor(并查集,DFS)
- Codeforces 452E Three strings 后缀数组 + 并查集
- CodeForces 437D -- The Child and Zoo(并查集+贪心)
- 并查集 codeforces party
- CodeForces 151D Quantity of Strings(并查集)
- 【打CF,学算法——四星级】CodeForces 455C Civilization (【详解】并查集+树的直径)
- codeforces 731C (并查集 水)
- codeforces 722c 用并查集求缺省的最大子序列
- Codeforces 699D Fix a Tree 并查集
- codeforces 731c Socks 并查集
- 【贪心+并查集】Codeforces 853A Planning
- CodeForces - 731C Socks(并查集)(贪心)
- Codeforces 870E 并查集 解题报告
- CodeForces - 698B Fix a Tree(并查集)
- Codeforces 292D Connected Components (并查集)
- codeforces 687D Dividing Kingdom II 并查集
- CodeForces 217A Ice Skating(并查集思路)
- Codeforces 469D Two Sets【思维+并查集】
- CodeForces 788B 浅谈并查集维护奇妙欧拉回路