CF1537F.Figure Fixing

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You have a connected undirected graph made of nn nodes and mm edges. The ii-th node has a value viv_i and a target value tit_i.

In an operation, you can choose an edge (i,j)(i, j) and add kk to both viv_i and vjv_j, where kk can be any integer. In particular, kk can be negative.

Your task to determine if it is possible that by doing some finite number of operations (possibly zero), you can achieve for every node ii, vi=tiv_i = t_i.

你有一个由 nn 个节点和 mm 条边构成的连通无向图。第 ii 个节点具有一个值 viv_i 和一个目标值 tit_i。

在一次操作中,你可以选择一条边 (i,j)(i, j),并将任意整数 kk 同时加到 viv_i 和 vjv_j 上(特别地,kk 可以为负数)。

你的任务是判断:是否可以通过执行有限次(可能为零次)上述操作,使得对每个节点 ii,均有 vi=tiv_i = t_i。

输入格式

The first line contains a single integer tt (1≤t≤10001 \leq t \leq 1000), the number of test cases. Then the test cases follow.

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

The second line contains nn integers v1…,vnv_1\ldots, v_n (−109≤vi≤109-10^9 \leq v_i \leq 10^9) — initial values of nodes.

The third line contains nn integers t1…,tnt_1\ldots, t_n (−109≤ti≤109-10^9 \leq t_i \leq 10^9) — target values of nodes.

Each of the next mm lines contains two integers ii and jj representing an edge between node ii and node jj (1≤i,j≤n1 \leq i, j \leq n, i≠ji\ne j).

It is guaranteed that the graph is connected and there is at most one edge between the same pair of nodes.

It is guaranteed that the sum of nn over all testcases does not exceed 2⋅1052 \cdot 10^5 and the sum of mm over all testcases does not exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤10001 \leq t \leq 1000),表示测试用例的数量。接下来是各测试用例。

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n≤2⋅1052 \leq n \leq 2\cdot 10^5,n−1≤m≤min⁡(2⋅105,n(n−1)2)n-1\leq m\leq \min(2\cdot 10^5, \frac{n(n-1)}{2})),分别表示节点数和边数。

第二行包含 nn 个整数 v1,…,vnv_1, \ldots, v_n(−109≤vi≤109-10^9 \leq v_i \leq 10^9)—— 各节点的初始值。

第三行包含 nn 个整数 t1,…,tnt_1, \ldots, t_n(−109≤ti≤109-10^9 \leq t_i \leq 10^9)—— 各节点的目标值。

接下来的 mm 行中,每行包含两个整数 ii 和 jj,表示节点 ii 与节点 jj 之间存在一条边(1≤i,j≤n1 \leq i, j \leq n,i≠ji \ne j)。

保证该图是连通的,且任意一对节点之间至多只有一条边。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5,且所有测试用例的 mm 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, if it is possible for every node to reach its target after some number of operations, print "YES". Otherwise, print "NO".

对于每个测试用例,如果经过若干次操作后,每个节点均能到达其目标位置,则输出 "YES";否则,输出 "NO"。

输入输出样例

  • 输入#1

    2
    4 4
    5 1 2 -3
    3 3 10 1
    1 2
    1 4
    3 2
    3 4
    4 4
    5 8 6 6
    -3 1 15 4
    1 2
    1 4
    3 2
    3 4

    输出#1

    YES
    NO

说明/提示

Here is a visualization of the first test case (the orange values denote the initial values and the blue ones the desired values):

One possible order of operations to obtain the desired values for each node is the following:

  • Operation 11: Add 22 to nodes 22 and 33.
  • Operation 22: Add −2-2 to nodes 11 and 44.
  • Operation 33: Add 66 to nodes 33 and 44.

Now we can see that in total we added −2-2 to node 11, 22 to node 22, 88 to node 33 and 44 to node 44 which brings each node exactly to it's desired value.

For the graph from the second test case it's impossible to get the target values.

以下是第一个测试用例的可视化图示(橙色数值表示初始值,蓝色数值表示目标值):

一种可实现各节点目标值的操作顺序如下:

  • 操作 11:向节点 22 和 33 各加 22。
  • 操作 22:向节点 11 和 44 各加 −2-2。
  • 操作 33:向节点 33 和 44 各加 66。

此时可以看出,节点 11 总共被加上了 −2-2,节点 22 总共被加上了 22,节点 33 总共被加上了 88,节点 44 总共被加上了 44,从而使每个节点的值恰好达到其目标值。

对于第二个测试用例中的图,无法得到目标值。

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

首页