#P4079. [SDOI2016] 齿轮
[SDOI2016] 齿轮
Description
There is a transmission system consisting of compound gears and chains. Each chain connects two compound gears and , and provides a transmission ratio , meaning that if we consider only these two gears, when gear turns turns, gear will turn turns. A positive ratio means that if gear turns clockwise, then gear also turns clockwise. A negative ratio means that if gear turns clockwise, then gear turns counterclockwise. If the transmission ratios provided by different chains are incompatible, some gears cannot turn. We want to know whether all compound gears in the system can rotate simultaneously.
This transmission system might be disconnected. That is, it is not guaranteed that any two gears are connected directly or indirectly by chains.
Input Format
There are multiple test cases.
The first line contains an integer , the total number of test cases, followed by test cases.
For each test case, the first line contains two integers and , the number of gears and the number of chains.
Then there are lines, each describing one chain. Each line contains four integers , meaning that, considering only this linkage, when gear turns turns, gear will turn turns. It is guaranteed that is a positive integer and is a nonzero integer. Note that can be negative.
Output Format
Output lines, one for each test case. First, output an identifier indicating the test case index, as shown in the sample output. Then output the decision: if all compound gears can run simultaneously, output Yes; otherwise, output No.
2
3 3
1 2 3 5
2 3 5 -7
1 3 3 -7
3 3
1 2 3 5
2 3 5 -7
1 3 3 7
Case #1: Yes
Case #2: No
Hint
Constraints
- .
- .
- .
- .
Translated by ChatGPT 5
京公网安备 11011102002149号