Under construction

Still being built. Things may change

Infiltration

Time limit1sMemory limit1024 MB

Summary
Decide whether Choi can walk from (1, 1) to (N, M) without touching a laser or being caught by a robot that follows one second behind.
Level

Medium7 of 10

Topics
BFS, Graph, Implementation
Solved
No attempts yet

Problem

On a dark night, Corporal Choi, an infiltration specialist, is carrying out a secret operation to sneak into an enemy base and steal their secrets.

The enemy base is a two-dimensional grid with NN rows and MM columns. Choi starts at the entrance (1,1)(1, 1) and must move to the target (N,M)(N, M). Every second, Choi moves one cell up, down, left, or right, or stays in place.

Laser sensors are installed in the base to detect intruders. Each sensor is placed on the boundary between two adjacent rows and fires a laser to the left or to the right. A laser keeps extending until it reaches a wall or another laser sensor. At most 2 sensors can be installed on the same boundary. If Choi touches a fired laser or steps on a sensor, Choi is detected and the operation fails.

1 second after Choi enters at the entrance, an autonomous security robot follows Choi into the entrance. Like Choi, the robot moves one cell up, down, left, or right, or stays in place every second. Unlike Choi, the robot is unaffected when it touches a laser sensor while moving. If the robot and Choi are in the same cell, Choi is detected and the operation fails.

Choi always prefers the safest possible infiltration. So Choi wants to find a path that reaches (N,M)(N, M) without being detected, no matter how the robot moves.

Input

The first line contains the integers NN and MM, the number of rows and columns of the base, separated by a space. (2≤N≤100 0002\leq N\leq 100\,000, 2≤M≤1092\leq M\leq 10^9)

From the second line through the NNth line, the information for the N−1N-1 boundaries is given. For the boundary between row ii and row i+1i+1, the first value is the number of laser sensors xix_i on that boundary, followed by xix_i pairs of a sensor column cijc_{ij} and a direction dijd_{ij}, separated by spaces. If dijd_{ij} is L, the sensor faces left. If dijd_{ij} is R, the sensor faces right. (0≤xi≤20\leq x_i\leq 2, 1≤cij≤M1\leq c_{ij}\leq M, cij<ci(j+1)c_{ij} < c_{i(j+1)})

Each laser sensor is placed so that, until it reaches a wall or another laser sensor, it detects every column in its facing direction, including its own column. No input places two different laser sensors at the same position.

Output

Print YES if there is a path that lets Choi reach (N,M)(N, M) without being detected by the robot or the laser sensors. Otherwise, print NO.

Examples2

  1. Example 1

    Input
    10 8
    0
    2 1 R 3 L
    1 5 R
    1 4 L
    0
    2 5 R 7 L
    0
    0
    1 7 L
    
    Expected output
    YES
    
  2. Example 2

    Input
    8 8
    2 1 R 4 L
    2 2 L 6 R
    0
    2 3 L 5 L
    2 7 R 8 R
    1 7 R
    1 8 L
    
    Expected output
    NO