#P4079. Composite Gear Transmission Consistency
Composite Gear Transmission Consistency
Composite Gear Transmission Consistency
You are given a transmission system consisting of N composite gears and M chains. Each chain connects two composite gears u and v and provides a transmission ratio of \( x:y \). This means that if we only consider these two gears, when gear \( u \) rotates \( x \) turns, gear \( v \) rotates \( y \) turns. Moreover, a positive transmission ratio indicates that if gear \( u \) rotates clockwise then gear \( v \) also rotates clockwise; a negative ratio indicates that gear \( v \) rotates counterclockwise when gear \( u \) rotates clockwise.
The system is consistent if there exists an assignment of rotation speeds (with directions) for each gear that satisfies all the given chain constraints. Otherwise, if the transmission ratios conflict with each other, some gears cannot rotate simultaneously.
Your task is to determine whether all N gears can rotate simultaneously.
Note: You may assume that the gears are numbered from 1 to N. For each chain, the effective constraint between gear \( u \) and gear \( v \) can be formulated as:
[ r[v] = \begin{cases} \frac{y}{x} \times r[u] & \text{if } x \text{ and } y \text{ have the same sign},\[6mm] -\frac{|y|}{|x|} \times r[u] & \text{if they have opposite signs}, \end{cases} ]
Using these constraints over all chains, check if a consistent assignment exists.
inputFormat
The first line contains two integers \( N \) and \( M \) (1 \( \leq N, M \leq 10^5 \) or as specified), representing the number of gears and the number of chains respectively.
Each of the following \( M \) lines contains four integers \( u, v, x, y \), meaning that a chain connects gear \( u \) and gear \( v \) with a transmission ratio \( x:y \). It is guaranteed that \( x \) is non-zero.
outputFormat
Output a single line containing YES
if all gears can rotate simultaneously consistently; otherwise, output NO
.
sample
3 3
1 2 1 2
2 3 2 1
1 3 1 1
YES