CF1850H.The Third Letter

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

In order to win his toughest battle, Mircea came up with a great strategy for his army. He has nn soldiers and decided to arrange them in a certain way in camps. Each soldier has to belong to exactly one camp, and there is one camp at each integer point on the xx-axis (at points ⋯ ,−2,−1,0,1,2,⋯\cdots, -2, -1, 0, 1, 2, \cdots).

The strategy consists of mm conditions. Condition ii tells that soldier aia_i should belong to a camp that is situated did_i meters in front of the camp that person bib_i belongs to. (If di<0d_i \lt 0, then aia_i's camp should be −di-d_i meters behind bib_i's camp.)

Now, Mircea wonders if there exists a partition of soldiers that respects the condition and he asks for your help! Answer "YES" if there is a partition of the nn soldiers that satisfies all of the mm conditions and "NO" otherwise.

Note that two different soldiers may be placed in the same camp.

为了赢得最艰难的一场战役,米尔恰为他的军队制定了一项精妙的战略。他有 nn 名士兵,并决定以某种方式将他们安排在营地中。每名士兵必须且只能属于一个营地,而 xx 轴上的每个整数点(即点 ⋯ ,−2,−1,0,1,2,⋯\cdots, -2, -1, 0, 1, 2, \cdots)都设有一个营地。

该战略包含 mm 条约束条件。第 ii 条约束条件指出:士兵 aia_i 所属的营地必须位于士兵 bib_i 所属营地前方 did_i 米处。(若 di<0d_i < 0,则表示 aia_i 所属营地应在 bib_i 所属营地后方 −di-d_i 米处。)

现在,米尔恰想知道是否存在一种士兵分组方案,使其满足全部约束条件,并请求你的帮助!若存在一种将 nn 名士兵划分到营地的方案,使得全部 mm 条约束均被满足,则输出 "YES";否则输出 "NO"。

注意:两名不同的士兵可以被安排在同一个营地。

输入格式

The first line contains a single integer tt (1≤t≤1001 \leq t \leq 100) — the number of test cases.

The first line of each test case contains two positive integers nn and mm (2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5; 1≤m≤n1 \leq m \leq n) — the number of soldiers, and the number of conditions respectively.

Then mm lines follow, each of them containing 33 integers: aia_i, bib_i, did_i (ai≠bia_i \neq b_i; 1≤ai,bi≤n1 \leq a_i, b_i \leq n; −109≤di≤109-10^9 \leq d_i \leq 10^9) — denoting the conditions explained in the statement. Note that if did_i is positive, aia_i should be did_i meters in front of bib_i and if it is negative, aia_i should be −di-d_i meters behind bib_i.

Note that the sum of nn over all test cases doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1001 \leq t \leq 100),表示测试用例的数量。

每个测试用例的第一行包含两个正整数 nn 和 mm(2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5;1≤m≤n1 \leq m \leq n),分别表示士兵的数量和条件的数量。

接下来是 mm 行,每行包含 33 个整数:aia_i、bib_i、did_i(ai≠bia_i \neq b_i;1≤ai,bi≤n1 \leq a_i, b_i \leq n;−109≤di≤109-10^9 \leq d_i \leq 10^9),表示题目陈述中所述的条件。注意:若 did_i 为正,则 aia_i 应位于 bib_i 前方 did_i 米处;若 did_i 为负,则 aia_i 应位于 bib_i 后方 −di-d_i 米处。

注意:所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output "YES" if there is an arrangement of the nn soldiers that satisfies all of the mm conditions and "NO" otherwise.

对于每个测试用例,如果存在一种 nn 名士兵的排列方式满足全部 mm 个条件,则输出 “YES”,否则输出 “NO”。

输入输出样例

  • 输入#1

    4
    5 3
    1 2 2
    2 3 4
    4 2 -6
    6 5
    1 2 2
    2 3 4
    4 2 -6
    5 4 4
    3 5 100
    2 2
    1 2 5
    1 2 4
    4 1
    1 2 3

    输出#1

    YES
    NO
    NO
    YES

说明/提示

For the first test case, we can partition the soldiers into camps in the following way: soldier:

  • Soldier 11 in the camp with the coordinate x=3x = 3.
  • Soldier 22 in the camp with the coordinate x=5x = 5.
  • Soldier 33 in the camp with the coordinate x=9x = 9.
  • Soldier 44 in the camp with the coordinate x=11x = 11.

For the second test case, there is no partition that can satisfy all the constraints at the same time.

For the third test case, there is no partition that satisfies all the constraints since we get contradictory information about the same pair.

For the fourth test case, in order to satisfy the only condition, a possible partition is:

  • Soldier 11 in the camp with the coordinate x=10x = 10.
  • Soldier 22 in the camp with the coordinate x=13x = 13.
  • Soldier 33 in the camp with the coordinate x=−2023x = -2023.
  • Soldier 44 in the camp with the coordinate x=−2023x = -2023.

对于第一个测试用例,我们可以将士兵按如下方式分配到营地中:

  • 士兵 11 分配到坐标为 x=3x = 3 的营地;
  • 士兵 22 分配到坐标为 x=5x = 5 的营地;
  • 士兵 33 分配到坐标为 x=9x = 9 的营地;
  • 士兵 44 分配到坐标为 x=11x = 11 的营地。

对于第二个测试用例,不存在一种分配方案能同时满足所有约束条件。

对于第三个测试用例,不存在满足所有约束条件的分配方案,因为我们关于同一对士兵得到了相互矛盾的信息。

对于第四个测试用例,为满足唯一约束条件,一种可行的分配方案是:

  • 士兵 11 分配到坐标为 x=10x = 10 的营地;
  • 士兵 22 分配到坐标为 x=13x = 13 的营地;
  • 士兵 33 分配到坐标为 x=−2023x = -2023 的营地;
  • 士兵 44 分配到坐标为 x=−2023x = -2023 的营地。

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

首页