码迷,mamicode.com
首页 > 其他好文 > 详细

Rumor

时间:2020-02-17 18:10:55      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:visit   sample   std   The   eve   algo   pairs   contain   lower   

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 ≤ nxi ≠ 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
Copy
5 2
2 5 3 4 8
1 4
4 5
output
Copy
10
input
Copy
10 0
1 2 3 4 5 6 7 8 9 10
output
Copy
55
input
Copy
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
output
Copy
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 <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#define ll long long
using namespace std;
int dir[4][2] = { {0,1},{0,-1},{-1,0},{1,0} };
vector<vector<int>> g;
vector<int> v;
vector<bool> visited;
int toadd;
void dfs(int pos)
{
    if (g[pos].size() == 0)
        return;
    if (visited[pos])
        return;
    visited[pos] = true;
    toadd = min(toadd, v[pos]);
    for (int i = 0; i < g[pos].size(); i++)
    {
        dfs(g[pos][i]);
    }
}
int main()
{
    int n, m;
    ll sum = 0;
    cin >> n >> m;
    g.resize(n + 1);
    v.resize(n + 1);
    visited.resize(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> v[i];
    }
    while (m--)
    {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for (int i = 1; i <= n; i++)
    {
        if (visited[i])
            continue;
        toadd = v[i];
        dfs(i);
        sum += toadd;
    }
    cout << sum;
    //system("pause");
    return 0;
}

 

Rumor

标签:visit   sample   std   The   eve   algo   pairs   contain   lower   

原文地址:https://www.cnblogs.com/dealer/p/12322669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!