#P10969. Katu Puzzle

Katu Puzzle

Problem Description

A Katu Puzzle is given in the form of a directed graph G(V,E)G(V, E). Each edge e(a,b)e(a, b) is labeled with a Boolean operator op\text{op} (one of AND, OR, XOR) and an integer cc (0c10 \leq c \leq 1). If we can find a value XiX_i (0Xi10 \leq X_i \leq 1) for every vertex ViV_i such that for every edge e(a,b)e(a, b) labeled with op\text{op} and cc, the following formula holds:

Xa op Xb=cX_a \ \text{op} \ X_b = c

then this Katu Puzzle is solvable.

Given a Katu Puzzle, your task is to determine whether it is solvable.

Input Format

The first line contains two integers NN (1N1001 \leq N \leq 100) and MM (0M10,0000 \leq M \leq 10,000), representing the number of vertices and the number of edges, respectively.

In the next MM lines, each line contains three integers aa (0a<N0 \leq a < N), bb (0b<N0 \leq b < N), cc, and an operator op\text{op}, describing the edge.

Output Format

Output one line containing YES\texttt{YES} or NO\texttt{NO}.

4 4
0 1 1 AND
1 2 1 OR
3 2 0 AND
3 0 0 XOR
YES

Hint

Translated by ChatGPT 5