#P1993. Farms and Crops Constraints Analysis

    ID: 15275 Type: Default 1000ms 256MiB

Farms and Crops Constraints Analysis

Farms and Crops Constraints Analysis

In this problem, you are given n farms and m memory records about the amount of crops planted in these farms. Each record is one of the following three types:

  • Farm a has at least c more crops than Farm b. (i.e. \(x_a - x_b \ge c\))
  • Farm a has at most c more crops than Farm b. (i.e. \(x_a - x_b \le c\))
  • Farm a has exactly the same number of crops as Farm b. (i.e. \(x_a - x_b = 0\))

Your task is to determine whether there exists an assignment of crop counts \(x_1, x_2, \dots, x_n\) (which can be any integers) to the farms such that all the given records are satisfied.

Input Format: The first line contains two integers n and m. Each of the following m lines contains four integers t, a, b, and c, representing a record. Here, t represents the type of the record:

  • If t = 1: Farm a has at least c more crops than Farm b (i.e. \(x_a - x_b \ge c\)).
  • If t = 2: Farm a has at most c more crops than Farm b (i.e. \(x_a - x_b \le c\)).
  • If t = 3: Farm a and Farm b grow the same number of crops (i.e. \(x_a = x_b\)). The value of c in this case will be 0.

Output Format: Output a single line containing "YES" if there is a valid assignment, or "NO" if there is no assignment that satisfies all records.

inputFormat

The first line contains two integers n and m separated by a space.

Each of the following m lines contains four space-separated integers: t, a, b, and c, describing a record as follows:

  • If t = 1: Farm a has at least c more crops than Farm b.
  • If t = 2: Farm a has at most c more crops than Farm b.
  • If t = 3: Farm a and Farm b grow the same number of crops (here, c will be 0).

Note that farms are numbered from 1 to n.

outputFormat

Output a single line containing either "YES" or "NO". "YES" indicates that there exists an assignment of integers \(x_1, x_2, \dots, x_n\) to the farms satisfying all constraints, and "NO" indicates that no such assignment exists.

sample

3 3
1 2 1 1
2 3 2 1
3 1 3 0
YES