D - Diagonal Separation 解説 /

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 425425

問題文

NNNN 列のグリッドがあります。高橋君は以下の条件を満たすように各マスを黒または白のいずれかに塗り分けたいと考えています。

  • すべての行について以下の条件が成り立つ。
    • ある整数 i (0iN)i\ (0\leq i\leq N) が存在して、その行の左から ii 個のマスは黒、それ以外は白で塗られている。
  • すべての列について以下の条件が成り立つ。
    • ある整数 i (0iN)i\ (0\leq i\leq N) が存在して、その列の上から ii 個のマスは黒、それ以外は白で塗られている。

MM 個のマスがすでに塗られています。そのうち ii 個目は上から XiX_i 行目、左から YiY_i 列目のマスで、CiC_iB のとき黒で、 W のとき白で塗られています。

高橋君はまだ塗られていない残りの N2MN^2-M 個のマスの色をうまく決めることで条件を満たすことができるか判定してください。

制約

  • 1N1091\leq N\leq 10^9
  • 1Mmin(N2,2×105)1\leq M\leq \min(N^2,2\times 10^5)
  • 1Xi,YiN1\leq X_i,Y_i\leq N
  • (Xi,Yi)(Xj,Yj) (ij)(X_i,Y_i)\neq (X_j,Y_j)\ (i\neq j)
  • CiC_iB または W
  • 入力される数値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

NN MM
X1X_1 Y1Y_1 C1C_1
\vdots
XMX_M YMY_M CMC_M

出力

条件を満たすことができるとき Yes を、そうでないとき No を出力せよ。


入力例 1Copy

Copy
4 3
4 1 B
3 2 W
1 3 B

出力例 1Copy

Copy
Yes

例えば以下の図のように色を塗り分けると条件を満たすことができます。すでに塗られているマスを赤色の線で囲んでいます。


入力例 2Copy

Copy
2 2
1 2 W
2 2 B

出力例 2Copy

Copy
No

塗られていない残りの 22 つのマスをどのように塗っても,条件を満たすことはできません。


入力例 3Copy

Copy
1 1
1 1 W

出力例 3Copy

Copy
Yes

入力例 4Copy

Copy
2289 10
1700 1083 W
528 967 B
1789 211 W
518 1708 W
1036 779 B
136 657 B
759 1497 B
902 1309 B
1814 712 B
936 763 B

出力例 4Copy

Copy
No

Score : 425425 points

Problem Statement

There is an N×NN \times N grid. Takahashi wants to color each cell black or white so that all of the following conditions are satisfied:

  • For every row, the following condition holds:
    • There exists an integer i (0iN)i\ (0\leq i\leq N) such that the leftmost ii cells are colored black, and the rest are colored white.
  • For every column, the following condition holds:
    • There exists an integer i (0iN)i\ (0\leq i\leq N) such that the topmost ii cells are colored black, and the rest are colored white.

Out of these N2N^2 cells, MM of them have already been colored. Among them, the ii-th one is at the XiX_i-th row from the top and the YiY_i-th column from the left, and it is colored black if CiC_i is B and white if CiC_i is W.

Determine whether he can color the remaining uncolored N2MN^2 - M cells so that all the conditions are satisfied.

Constraints

  • 1N1091\leq N\leq 10^9
  • 1Mmin(N2,2×105)1\leq M\leq \min(N^2,2\times 10^5)
  • 1Xi,YiN1\leq X_i,Y_i\leq N
  • (Xi,Yi)(Xj,Yj) (ij)(X_i,Y_i)\neq (X_j,Y_j)\ (i\neq j)
  • CiC_i is B or W.
  • All input numbers are integers.

Input

The input is given from Standard Input in the following format:

NN MM
X1X_1 Y1Y_1 C1C_1
\vdots
XMX_M YMY_M CMC_M

Output

If it is possible to satisfy the conditions, print Yes; otherwise, print No.


Sample Input 1Copy

Copy
4 3
4 1 B
3 2 W
1 3 B

Sample Output 1Copy

Copy
Yes

For example, one can color the grid as in the following figure to satisfy the conditions. The cells already colored are surrounded by red borders.


Sample Input 2Copy

Copy
2 2
1 2 W
2 2 B

Sample Output 2Copy

Copy
No

No matter how the remaining two cells are colored, the conditions cannot be satisfied.


Sample Input 3Copy

Copy
1 1
1 1 W

Sample Output 3Copy

Copy
Yes

Sample Input 4Copy

Copy
2289 10
1700 1083 W
528 967 B
1789 211 W
518 1708 W
1036 779 B
136 657 B
759 1497 B
902 1309 B
1814 712 B
936 763 B

Sample Output 4Copy

Copy
No


2025-03-14 (金)
05:16:37 +00:00