#P4079. [SDOI2016] 齿轮

    ID: 2984 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>搜索2016并查集各省省选山东逆元

[SDOI2016] 齿轮

Description

There is a transmission system consisting of NN compound gears and MM chains. Each chain connects two compound gears uu and vv, and provides a transmission ratio x:yx:y, meaning that if we consider only these two gears, when gear uu turns xx turns, gear vv will turn yy turns. A positive ratio means that if gear uu turns clockwise, then gear vv also turns clockwise. A negative ratio means that if gear uu turns clockwise, then gear vv turns counterclockwise. If the transmission ratios provided by different chains are incompatible, some gears cannot turn. We want to know whether all NN 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 TT, the total number of test cases, followed by TT test cases.

For each test case, the first line contains two integers NN and MM, the number of gears and the number of chains.

Then there are MM lines, each describing one chain. Each line contains four integers u,v,x,yu, v, x, y, meaning that, considering only this linkage, when gear uu turns xx turns, gear vv will turn yy turns. It is guaranteed that xx is a positive integer and yy is a nonzero integer. Note that yy can be negative.

Output Format

Output TT 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 NN 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

  • 1T321\leq T\leq 32.
  • 1N10001\leq N\leq 1000.
  • 1M1041\leq M\leq 10^4.
  • 1x,y1001\leq x,|y|\leq 100.

Translated by ChatGPT 5