A15783 | Monsters
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There is an undirected graph with $n$ vertices and $m$ edges. Initially, for each vertex $i$ , there is a monster with danger $a_{i}$ on that vertex. For a monster with danger $a_{i}$ , you can defeat it if and only if you have defeated at least $a_{i}$ other monsters before.
Now you want to defeat all the monsters. First, you choose some vertex $s$ and defeat the monster on that vertex (since you haven't defeated any monsters before, $a_{s}$ has to be $0$ ). Then, you can move through the edges. If you want to move from vertex $u$ to vertex $v$ , then the following must hold: either the monster on vertex $v$ has been defeated before, or you can defeat it now. For the second case, you defeat the monster on vertex $v$ and reach vertex $v$ .
You can pass the vertices and the edges any number of times. Determine whether you can defeat all the monsters or not.
Now you want to defeat all the monsters. First, you choose some vertex $s$ and defeat the monster on that vertex (since you haven't defeated any monsters before, $a_{s}$ has to be $0$ ). Then, you can move through the edges. If you want to move from vertex $u$ to vertex $v$ , then the following must hold: either the monster on vertex $v$ has been defeated before, or you can defeat it now. For the second case, you defeat the monster on vertex $v$ and reach vertex $v$ .
You can pass the vertices and the edges any number of times. Determine whether you can defeat all the monsters or not.
输入格式
Each test contains multiple test cases. The first line contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of test cases. Their description follows.
The first line of each test case contains two integers $n$ , $m$ ( $1 \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 $n$ integers $a_{1}, a_{2}, \ldots, a_{n}$ ( $0 \le a_{i} \le n$ ) — the dangers of monsters on corresponding vertices.
For the following $m$ lines, each line contains two integers $u$ , $v$ ( $1 \le u, v \le n$ ), describing an edge connecting vertex $u$ and vertex $v$ . It is guaranteed that there are no multi-edges or self-loops in the graph.
It is guaranteed that both the sum of $n$ and the sum of $m$ over all test cases do not exceed $2 \cdot 10^5$ .
The first line of each test case contains two integers $n$ , $m$ ( $1 \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 $n$ integers $a_{1}, a_{2}, \ldots, a_{n}$ ( $0 \le a_{i} \le n$ ) — the dangers of monsters on corresponding vertices.
For the following $m$ lines, each line contains two integers $u$ , $v$ ( $1 \le u, v \le n$ ), describing an edge connecting vertex $u$ and vertex $v$ . It is guaranteed that there are no multi-edges or self-loops in the graph.
It is guaranteed that both the sum of $n$ and the sum of $m$ over all test cases do not exceed $2 \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.
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.
输入输出样例
输入 #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 $3$ and defeat the monster on it, before you go to vertices $2$ , $1$ in this order, defeating the monsters on them as well. Then you return to vertex $3$ , and go to vertex $4$ , defeating the monster on it.
In the third test case, there is no path to vertex $4$ if you start at vertex $1$ . Also, there is no path to vertices $1$ , $2$ , and $3$ if you start at vertex $4$ .
In the third test case, there is no path to vertex $4$ if you start at vertex $1$ . Also, there is no path to vertices $1$ , $2$ , and $3$ if you start at vertex $4$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted