CF1810E.Monsters

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There is an undirected graph with nn vertices and mm edges. Initially, for each vertex ii, there is a monster with danger aia_{i} on that vertex. For a monster with danger aia_{i}, you can defeat it if and only if you have defeated at least aia_{i} other monsters before.

Now you want to defeat all the monsters. First, you choose some vertex ss and defeat the monster on that vertex (since you haven't defeated any monsters before, asa_{s} has to be 00). Then, you can move through the edges. If you want to move from vertex uu to vertex vv, then the following must hold: either the monster on vertex vv has been defeated before, or you can defeat it now. For the second case, you defeat the monster on vertex vv and reach vertex vv.

You can pass the vertices and the edges any number of times. Determine whether you can defeat all the monsters or not.

有一个包含 nn 个顶点和 mm 条边的无向图。初始时,每个顶点 ii 上都有一只危险度为 aia_{i} 的怪物。对于危险度为 aia_{i} 的怪物,当且仅当你此前已击败至少 aia_{i} 只怪物时,才能击败它。

现在你想击败所有怪物。首先,你选择某个顶点 ss 并击败其上的怪物(由于此时你尚未击败任何怪物,因此必须满足 as=0a_{s} = 0)。之后,你可以沿边移动。若你想从顶点 uu 移动到顶点 vv,则必须满足以下条件之一:

  • 顶点 vv 上的怪物已被击败;
  • 或者你当前可以击败它(即你此前已击败的怪物数量 ≥av\ge a_v);在后一种情况下,你将击败顶点 vv 上的怪物,并到达顶点 vv。

你可以任意多次经过顶点和边。请判断是否能够击败所有怪物。

输入格式

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. Their description follows.

The first line of each test case contains two integers nn, mm (1≤n,m≤2⋅1051 \le n, m \le 2 \cdot 10^5) — the number of vertices and edges in the graph respectively.

The second line of each test case contains nn integers a1,a2,…,ana_{1}, a_{2}, \ldots, a_{n} (0≤ai≤n0 \le a_{i} \le n) — the dangers of monsters on corresponding vertices.

For the following mm lines, each line contains two integers uu, vv (1≤u,v≤n1 \le u, v \le n), describing an edge connecting vertex uu and vertex vv. It is guaranteed that there are no multi-edges or self-loops in the graph.

It is guaranteed that both the sum of nn and the sum of mm over all test cases do not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn、mm(1≤n,m≤2⋅1051 \le n, m \le 2 \cdot 10^5),分别表示图中的顶点数和边数。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_{1}, a_{2}, \ldots, a_{n}(0≤ai≤n0 \le a_{i} \le n),表示对应顶点上怪物的危险程度。

接下来的 mm 行中,每行包含两个整数 uu、vv(1≤u,v≤n1 \le u, v \le n),表示一条连接顶点 uu 和顶点 vv 的边。保证图中不存在重边或自环。

保证所有测试用例中 nn 的总和与 mm 的总和均不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output "YES" if you can defeat all the monsters, or "NO" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

对于每个测试用例,如果你能击败所有怪物,则输出 “YES”,否则输出 “NO”。

你可以以任意大小写形式输出答案(大写或小写均可)。例如,字符串 “yEs”、“yes”、“Yes” 和 “YES” 均会被识别为肯定回答。

输入输出样例

  • 输入#1

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

    输出#1

    YES
    YES
    NO
    YES
    NO

说明/提示

In the first test case, you can start at vertex 33 and defeat the monster on it, before you go to vertices 22, 11 in this order, defeating the monsters on them as well. Then you return to vertex 33, and go to vertex 44, defeating the monster on it.

In the third test case, there is no path to vertex 44 if you start at vertex 11. Also, there is no path to vertices 11, 22, and 33 if you start at vertex 44.

在第一个测试用例中,你可以从顶点 33 出发,先击败该顶点上的怪物,然后依次前往顶点 22、11,并击败它们上面的怪物。接着你返回顶点 33,再前往顶点 44,击败其上的怪物。

在第三个测试用例中,若从顶点 11 出发,则不存在通往顶点 44 的路径;同样地,若从顶点 44 出发,则不存在通往顶点 11、22 和 33 的路径。

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

首页