QOJ.ac

QOJ

Limite de temps : 2.0 s Limite de mémoire : 256 MB Points totaux : 100

#11631. 森林任务

Statistiques

给定一个包含 $N$ 个顶点和 $M$ 条边的森林。顶点编号为 $0$ 到 $N-1$。边以 $(x_i, y_i)$ 的形式给出,表示顶点 $x_i$ 和 $y_i$ 之间有一条边相连。

每个顶点 $i$ 被赋予一个权值 $a_i$。你需要在给定的森林中添加边,使得森林变得连通。添加一条边时,你需要选择两个不同的顶点 $i$ 和 $j$,然后在 $i$ 和 $j$ 之间连接一条边。该操作的代价为 $a_i + a_j$ 美元,且操作完成后,顶点 $i$ 和 $j$ 均不能再被选择。

求使森林连通所需的最小总代价,如果无法使森林连通,则输出 “Impossible”。

输入格式

输入按以下格式给出:

$N \ M$ $a_0 \ a_1 \ \dots \ a_{N-1}$ $x_1 \ y_1$ $\dots$ $x_M \ y_M$

数据范围: $1 \le N \le 100\,000$,$0 \le M \le N - 1$,$1 \le a_i \le 10^9$,$0 \le x_i, y_i \le N - 1$。给定的图是一个森林。所有输入值均为整数。

输出格式

输出使森林连通所需的最小总代价,如果无法使森林连通,则输出 “Impossible”。

样例

样例输入 1

7 5
1 2 3 4 5 6 7
3 0
4 0
1 2
1 3
5 6

样例输出 1

7

样例输入 2

5 0
3 1 4 1 5

样例输出 2

Impossible

样例输入 3

1 0
5

样例输出 3

0

说明

在样例 1 中,如果我们连接顶点 0 和 5,图将变得连通,代价为 $1 + 6 = 7$。

在样例 2 中,我们无法使图连通。

在样例 3 中,无论我们是否进行操作,图本身已经是连通的。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.