CF1850H.The Third Letter
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
In order to win his toughest battle, Mircea came up with a great strategy for his army. He has 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 -axis (at points ⋯,−2,−1,0,1,2,⋯ ).
The strategy consists of m conditions. Condition i tells that soldier ai should belong to a camp that is situated di meters in front of the camp that person bi belongs to. (If di<0 , then ai 's camp should be −di meters behind bi '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 soldiers that satisfies all of the 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 ( 1≤t≤100 ) — the number of test cases.
The first line of each test case contains two positive integers n and m ( 2≤n≤2⋅105 ; 1≤m≤n ) — the number of soldiers, and the number of conditions respectively.
Then m lines follow, each of them containing 3 integers: ai , bi , di ( ai=bi ; 1≤ai,bi≤n ; −109≤di≤109 ) — denoting the conditions explained in the statement. Note that if di is positive, ai should be di meters in front of bi and if it is negative, ai should be −di meters behind bi .
Note that the sum of n over all test cases doesn't exceed 2⋅105 .
输出格式
For each test case, output "YES" if there is an arrangement of the n soldiers that satisfies all of the m conditions and "NO" otherwise.
输入输出样例
输入#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 in the camp with the coordinate x=3 .
- Soldier 2 in the camp with the coordinate x=5 .
- Soldier 3 in the camp with the coordinate x=9 .
- Soldier 4 in the camp with the coordinate 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 in the camp with the coordinate x=10 .
- Soldier 2 in the camp with the coordinate x=13 .
- Soldier 3 in the camp with the coordinate x=−2023 .
- Soldier 4 in the camp with the coordinate x=−2023 .