CF893C.Rumor

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 c__i 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.

沃瓦曾向自己承诺,他绝不再玩电脑游戏……但最近,知名游戏开发公司 Firestorm 发布了他们的最新游戏《远境世界》(World of Farcraft),并迅速走红。当然,沃瓦立刻开始玩了起来。

现在,他正尝试完成一项任务:前往名为“上城”(Overcity)的定居点,并在其中散播一则谣言。

沃瓦知道上城中共有 $ n $ 个角色。其中一些角色互为朋友,朋友之间会共享各自获得的信息。此外,沃瓦还知道,他可以贿赂任意角色,使其主动散播该谣言;第 $ i $ 个角色要求 $ c_i $ 枚金币作为散播谣言的报酬。一旦某个角色听到了谣言,他便会免费将其告知自己所有的朋友,而这些朋友又会继续免费告知他们自己的朋友,依此类推。

当全部 $ n $ 个角色都得知该谣言时,任务即告完成。沃瓦为完成该任务所需支付的最少金币数量是多少?

若你认为尚未完全理解本题,请参阅题目末尾的“说明”部分。

输入格式

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 c__i (0 ≤ c__i ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.

Then m lines follow, each containing a pair of numbers (x__i, y__i) which represent that characters x__i and y__i are friends (1 ≤ x__i, y__i ≤ n, x__i ≠ y__i). It is guaranteed that each pair is listed at most once.

第一行包含两个整数 nn 和 mm(1 ≤ n ≤ 1051 \le n \le 10^5,0 ≤ m ≤ 1050 \le m \le 10^5)—— 分别表示 Overcity 中角色的数量以及朋友对的数量。

第二行包含 nn 个整数 cic_i(0 ≤ ci ≤ 1090 \le c_i \le 10^9)—— 表示第 ii 个角色为开始传播谣言所要求的金币数量。

接下来是 mm 行,每行包含一对数字 (xi, yi)(x_i,\,y_i),表示角色 xix_i 和 yiy_i 是朋友(1 ≤ xi, yi ≤ n1 \le x_i,\,y_i \le n,且 xi ≠ yix_i \ne y_i)。保证每对朋友至多出现一次。

输出格式

Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.

输出一个数字——Vova 为完成任务所需花费的最少金币数量。

输入输出样例

  • 输入#1

    5 2
    2 5 3 4 8
    1 4
    4 5

    输出#1

    10
  • 输入#2

    10 0
    1 2 3 4 5 6 7 8 9 10

    输出#2

    55
  • 输入#3

    10 5
    1 6 2 7 3 8 4 9 5 10
    1 2
    3 4
    5 6
    7 8
    9 10

    输出#3

    15

说明/提示

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.

在第一个例子中,最优策略是贿赂第一位角色(他将把谣言传播给第四位角色,而第四位角色再将其传播给第五位角色)。此外,Vova 还需贿赂第二位和第三位角色,以确保他们知晓该谣言。

在第二个例子中,Vova 必须贿赂所有人。

在第三个例子中,最优策略是贿赂第一位、第三位、第五位、第七位和第九位角色。

输入解题思路,AI测评打分。不知道怎么写?

首页