Educational codeforces Round C. Rumor

Source: Internet
Author: User
Tags integer numbers time limit


Original question:
C. Rumor
Time limit per test2 seconds
Memory limit per test256 megabytes
Inputstandard input
Outputstandard output
Vova promised himself that he would never play computer ... But recently firestorm-a well-known game developing company-published their newest game, world of Farcraft, and it bec Ame 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 is n characters in overcity. Some characters is friends to all 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-their friends (for free), and so on.



The quest is finished if all n characters know the rumor. What's the minimum amount of gold Vova needs to spend on order to finish the quest?



Take a look at the notes if you think haven ' t understood the problem completely.



Input
The first line contains-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 t He rumor.



Then M. lines follow, each containing a pair of numbers (xi, Yi) which represent, characters Xi and Yi is friends (1≤ Xi, Yi≤n, Xi≠yi). It is guaranteed, and each pair is listed at the most once.



Output
Print One number-the minimum amount of gold Vova have 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
The first example the best decision are to bribe the first character (he'll spread the rumor to fourth character, and The fourth one would spread it to fifth). Also Vova have to bribe the second and the third characters, so they know the rumor.



In the second example Vova have to bribe everyone.



In the third example the optimal decision are to bribe the first, the third, the fifth, the seventh and the ninth character S.



English:
There is a person to go to a city to spread rumors, the people in this city know each other, if one of the human people who know, then the circle of people will know. The cost to everyone to listen to rumors is Ai a_i, now give you n individuals, and these people know each other's relationship. Ask you at least how much it costs to let everyone know rumors.


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=100001;
int n,m;
int father[maxn];
ll Rank[maxn];


int Find(int x)
{
    if(father[x]==x)
        return father[x];
    else
        return father[x]=Find(father[x]);
}
void Union(int a,int b)
{
    int x=Find(a);
    int y=Find(b);
    if(Rank[x]>Rank[y])
        father[x]=y;
    else
        father[y]=x;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        memset(Rank,0,sizeof(Rank));
        for(int i=1;i<=n;i++)
        {
            father[i]=i;
            cin>>Rank[i];
        }
        int x,y;
        for(int i=1;i<=m;i++)
        {
            cin>>x>>y;
            Union(x,y);
        }
        for(int i=1;i<=n;i++)
            father[x]=Find(father[x]);
        ll ans=0;
        for(int i=1;i<=n;i++)
            if(father[i]==i)
                ans+=Rank[i];
        cout<<ans<<endl;
    }
    return 0;
}


Answer:



is actually the undirected graph to find all the connected components, and then the AI a_i value in each connected component to find the smallest to add together is the final result.
You can use a rank-and-check set, in which a person in a circle is connected to a root node, and the root node is updated to the minimum value of AI a_i each time.
Finally, the values of all the root nodes are added together.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.