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[][] = { {,},{,-},{-,},{,} };
vector<vector<int>> g;
vector<int> v;
vector<bool> visited;
int toadd;
void dfs(int pos)
{
if (g[pos].size() == )
return;
if (visited[pos])
return;
visited[pos] = true;
toadd = min(toadd, v[pos]);
for (int i = ; i < g[pos].size(); i++)
{
dfs(g[pos][i]);
}
}
int main()
{
int n, m;
ll sum = ;
cin >> n >> m;
g.resize(n + );
v.resize(n + );
visited.resize(n + );
for (int i = ; 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 = ; i <= n; i++)
{
if (visited[i])
continue;
toadd = v[i];
dfs(i);
sum += toadd;
}
cout << sum;
//system("pause");
return ;
}

Rumor的更多相关文章

  1. codeforces 893C Rumor 前向星+dfs

    893C Rumor 思路: 前向星+DFS 代码: #include <bits/stdc++.h> using namespace std; #define _for(i,a,b) f ...

  2. 缩点 CF893C Rumor

    CF893C Rumor 有n个人,其中有m对朋友,现在你有一个秘密你想告诉所有人,第i个人愿意出价a[i]买你的秘密,获得秘密的人会免费告诉它的所有朋友(他朋友的朋友也会免费知道),现在他们想出最少 ...

  3. Educational Codeforces Round 33 (Rated for Div. 2) C. Rumor【并查集+贪心/维护集合最小值】

    C. Rumor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  4. Modeling Conversation Structure and Temporal Dynamics for Jointly Predicting Rumor Stance and Veracity(ACL-19)

    记录一下,论文建模对话结构和时序动态来联合预测谣言立场和真实性及其代码复现. 1 引言 之前的研究发现,公众对谣言消息的立场是识别流行的谣言的关键信号,这也能表明它们的真实性.因此,对谣言的立场分类被 ...

  5. 谣言检测——《MFAN: Multi-modal Feature-enhanced Attention Networks for Rumor Detection》

    论文信息 论文标题:MFAN: Multi-modal Feature-enhanced Attention Networks for Rumor Detection论文作者:Jiaqi Zheng, ...

  6. 论文解读(RvNN)《Rumor Detection on Twitter with Tree-structured Recursive Neural Networks》

    论文信息 论文标题:Rumor Detection on Twitter with Tree-structured Recursive Neural Networks论文作者:Jing Ma, Wei ...

  7. 论文解读(FedGAT)《Federated Graph Attention Network for Rumor Detection》

    论文信息 论文标题:Federated Graph Attention Network for Rumor Detection论文作者:Huidong Wang, Chuanzheng Bai, Ji ...

  8. 谣言检测——(PSA)《Probing Spurious Correlations in Popular Event-Based Rumor Detection Benchmarks》

    论文信息 论文标题:Probing Spurious Correlations in Popular Event-Based Rumor Detection Benchmarks论文作者:Jiayin ...

  9. 谣言检测(GACL)《Rumor Detection on Social Media with Graph Adversarial Contrastive Learning》

    论文信息 论文标题:Rumor Detection on Social Media with Graph AdversarialContrastive Learning论文作者:Tiening Sun ...

随机推荐

  1. git客户端的常用命令

    注意:仓库只有管理员建的你才有权限上传,不然自己建的也没用,没权限上传 1.远程仓库路径查询 git remote -v 2.添加远程仓库 git remote add origin <你的项目 ...

  2. 安装Flink集群

    1.Windows安装 https://blog.csdn.net/clj198606061111/article/details/99694033 2.Linux安装 https://blog.cs ...

  3. ArcScene 创建三维模型数据

    1. 拉伸 添加面元素图层 在图层上右键----属性 , 设置拉伸值,可以输入固定值或者选择字段值. 2. 导入   3DMAX 的 3ds 文件,和 Google SketchUp 的skp文件, ...

  4. C#调用C++类库例子

    一.新建一个解决方案,并在解决方案下添加一个.netframework的项目,命名为FrameworkConsoleTest.再添加一个C++的动态链接库DLL项目,命名为EncryptBase. 二 ...

  5. C# 串口关闭时主界面卡死原因分析

    目录 问题描述 查找原因 SerialPort类Open()方法 SerialPort类Close()方法 死锁原因 解决死锁 总结 问题描述 前几天用SerialPort类写一个串口的测试程序,关闭 ...

  6. STL中_Rb_tree的探索

    我们知道STL中我们常用的set与multiset和map与multimap都是基于红黑树.本文介绍了它们的在STL中的底层数据结构_Rb_tree的直接用法与部分函数.难点主要是_Rb_tree的各 ...

  7. react-native构建基本页面2---轮播图+九宫格

    配置首页的轮播图 轮播图官网 运行npm i react-native-swiper --save安装轮播图组件 导入轮播图组件import Swiper from 'react-native-swi ...

  8. 跨域请求问题:CORS

    1.编写过滤器类:需要实现Filter接口,并重写三个方法: (1)先设置字符编码: request.setCharacterEncoding("utf-8"); response ...

  9. Struts2学习-struts执行过程简述

    1.web.xml <web-app> <filter> <filter-name>struts2</filter-name> <filter-c ...

  10. 微信小程序weui的使用

    大家好,我是前端菜鸟,大家可以叫我惊蛰,今天给大家分享一下在微信小程序中对weui的引入和使用,其他的也不再赘述,文中有不对的还请指正,谢谢. 直入主题: 1.下载weui 进入GitHub http ...