#P1850H. The Third Letter

    ID: 577 远端评测题 2000ms 256MiB 尝试: 8 已通过: 1 难度: 10 上传者: 标签>dfs and similardsugraphsgreedyimplementation*1700

The Third Letter

The Third Letter

题面翻译

tt 组数据。对于每组数据:

第一行给定 nnmm,表示有 nn 个人和 mm 个要求。每个人都站在数轴上的一个整数点上(可以在负半轴上)。

然后的 mm 行,每行一个要求。每个要求包含 ai,bia_i, b_idid_i,表示第 aia_i 个人要站在第 bib_i 个人前方的第 did_i 个位置(如果 did_i 是负数,则第 aia_i 个人要站在第 bib_i 个人后方的第 di-d_i 个位置)。问是否存在满足要求的方案。

$t \leq 100, \sum n \leq 2e5, m \leq n, -10^9 \leq d_i \leq 10^9$

题目描述

In order to win his toughest battle, Mircea came up with a great strategy for his army. He has n n 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 x x -axis (at points ,2,1,0,1,2, \cdots, -2, -1, 0, 1, 2, \cdots ).

The strategy consists of m m conditions. Condition i i tells that soldier ai a_i should belong to a camp that is situated di d_i meters in front of the camp that person bi b_i belongs to. (If di<0 d_i < 0 , then ai a_i 's camp should be di -d_i meters behind bi b_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 n n soldiers that satisfies all of the m m conditions and "NO" otherwise.

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

输入格式

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

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

Then m m lines follow, each of them containing 3 3 integers: ai a_i , bi b_i , di d_i ( aibi a_i \neq b_i ; 1ai,bin 1 \leq a_i, b_i \leq n ; 109di109 -10^9 \leq d_i \leq 10^9 ) — denoting the conditions explained in the statement. Note that if di d_i is positive, ai a_i should be di d_i meters in front of bi b_i and if it is negative, ai a_i should be di -d_i meters behind bi b_i .

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

输出格式

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

样例 #1

样例输入 #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 1 1 in the camp with the coordinate x=3 x = 3 .
  • Soldier 2 2 in the camp with the coordinate x=5 x = 5 .
  • Soldier 3 3 in the camp with the coordinate x=9 x = 9 .
  • Soldier 4 4 in the camp with the coordinate x=11 x = 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 1 1 in the camp with the coordinate x=10 x = 10 .
  • Soldier 2 2 in the camp with the coordinate x=13 x = 13 .
  • Soldier 3 3 in the camp with the coordinate x=2023 x = -2023 .
  • Soldier 4 4 in the camp with the coordinate x=2023 x = -2023 .