CF1851G.Vlad and the Mountains

普及+/提高

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Vlad decided to go on a trip to the mountains. He plans to move between nn mountains, some of which are connected by roads. The ii-th mountain has a height of hih_i.

If there is a road between mountains ii and jj, Vlad can move from mountain ii to mountain jj by spending hj−hih_j - h_i units of energy. If his energy drops below zero during the transition, he will not be able to move from mountain ii to mountain jj. Note that hj−hih_j - h_i can be negative and then the energy will be restored.

Vlad wants to consider different route options, so he asks you to answer the following queries: is it possible to construct some route starting at mountain aa and ending at mountain bb, given that he initially has ee units of energy?

弗拉德决定去山区旅行。他计划在 nn 座山之间移动,其中一些山由道路连接。第 ii 座山的高度为 hih_i。

如果山 ii 与山 jj 之间存在一条道路,则弗拉德可以从山 ii 移动到山 jj,消耗 hj−hih_j - h_i 单位的能量。若在移动过程中他的能量降至零以下,则他将无法从山 ii 移动到山 jj。注意,hj−hih_j - h_i 可能为负数,此时能量将得到恢复。

弗拉德希望考虑不同的路线方案,因此请你回答如下查询:给定初始能量为 ee,是否存在一条从山 aa 出发、到达山 bb 的路径?

输入格式

The first line of the input contains an integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The descriptions of the test cases follow.

The first line of each test case contains two numbers nn and mm (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5, 1≤m≤min⁡(n⋅(n−1)2,2⋅105)1 \le m \le \min(\frac{n\cdot(n - 1)}{2}, 2 \cdot 10^5)) — the number of mountains and the number of roads between them, respectively.

The second line contains nn integers h1,h2,h3,…,hnh_1, h_2, h_3, \dots, h_n (1≤hi≤1091 \le h_i \le 10^9) — the heights of the mountains.

The next mm lines contain two integers uu and vv (1≤u,v≤n1 \le u, v \le n, u≠vu \ne v) — the numbers of the mountains connected by a road. It is guaranteed that no road appears twice.

The next line contains an integer qq (1≤q≤2⋅1051 \le q \le 2 \cdot 10^5) — the number of queries.

The following qq lines contain three numbers aa, bb, and ee (1≤a,b≤n1 \le a, b \le n, 0≤e≤1090 \le e \le 10^9) — the initial and final mountains of the route, and the amount of energy, respectively.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5. The same guarantee applies to mm and qq.

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5,1≤m≤min⁡(n⋅(n−1)2,2⋅105)1 \le m \le \min(\frac{n\cdot(n - 1)}{2}, 2 \cdot 10^5))—— 分别表示山峰的数量以及山峰之间的道路数量。

第二行包含 nn 个整数 h1,h2,h3,…,hnh_1, h_2, h_3, \dots, h_n(1≤hi≤1091 \le h_i \le 10^9)—— 表示各山峰的高度。

接下来的 mm 行每行包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n,u≠vu \ne v)—— 表示由一条道路连接的两座山峰的编号。保证不会出现重复的道路。

接下来一行包含一个整数 qq(1≤q≤2⋅1051 \le q \le 2 \cdot 10^5)—— 表示查询的数量。

接下来的 qq 行每行包含三个数 aa、bb 和 ee(1≤a,b≤n1 \le a, b \le n,0≤e≤1090 \le e \le 10^9)—— 分别表示路径的起点山峰、终点山峰以及能量值。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5;对 mm 和 qq 同样有此保证。

输出格式

For each query, output "YES" if Vlad can construct a route from mountain aa to mountain bb, and "NO" otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

In the examples below, the answers for different test cases are separated by an empty line, which you do not need to output.

对于每个查询,如果弗拉德能够构建一条从山 aa 到山 bb 的路径,则输出 "YES";否则输出 "NO"。

答案的大小写不限(例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均被视为肯定回答)。

在下面的示例中,不同测试用例的答案之间以空行分隔,但你无需输出该空行。

输入输出样例

  • 输入#1

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

    输出#1

    YES
    NO
    YES
    YES
    NO
    
    YES
    NO
    NO
    YES
    NO
  • 输入#2

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

    输出#2

    YES
    YES
    YES
    YES
    NO
    
    YES
    YES
    YES
    YES
    YES
  • 输入#3

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

    输出#3

    YES
    YES
    YES
    YES
    YES

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

首页