Under construction

Still being built. Things may change

Painted Corridors

Time limit5sMemory limit512 MB

Summary
Decide whether three robots starting at given junctions can paint every colored corridor, with secondary colors needing two robots to traverse together.
Level

Hard9 of 10

Topics
Graph, BFS, Union-find
Solved
No attempts yet

Problem

The Institute of Colorfully Painted Corridors is planning the construction of a new building. The building has many junctions and corridors, each connecting a pair of junctions. Painting robots drive along the corridors and paint the walls as they go. The architect has specified the colors of some corridors. The allowed colors are red, orange, yellow, green, blue, and purple.

The budget covers only three robots, so there is one robot for each primary color: red, yellow, and blue. These robots are the cheapest possible model and cannot turn their paint sprayer off. They can move as fast or as slow as they like, and they can stop entirely.

A corridor that needs a secondary color (orange, green, or purple) must be traversed in the same direction at the same time by the two robots with the matching primary colors. The mixing rules are: orange = red + yellow, green = yellow + blue, and purple = red + blue.

A corridor with no specified color (X) may be painted any color, or left unpainted. A corridor may be painted more than once, but each time it must receive the correct color. An unspecified corridor may take different colors on different passes. All corridors can be traveled in both directions. After painting all corridors, the robots may end at any junctions.

Input

The first line contains five integers nn (2≤n≤1002 \le n \le 100), mm (1≤m≤n⋅(n−1)21 \le m \le \frac{n \cdot (n-1)}{2}), rr, bb, and yy (1≤r,b,y≤n1 \le r, b, y \le n). Here nn is the number of junctions, mm is the number of corridors, and rr, bb, and yy are the starting junctions of the red, blue, and yellow robots. Junctions are numbered 1 through nn. Each of the next mm lines contains two integers ii and jj (1≤i<j≤n1 \le i < j \le n) and a character cc, which is one of R, O, Y, G, B, P, or X. This means there is a corridor between junctions ii and jj, and cc is its desired color: R (red), O (orange), Y (yellow), G (green), B (blue), P (purple), or X (unspecified). There is at most one corridor between any pair of junctions.

Output

Output a single integer: 1 if the corridors can be painted as described, and 0 otherwise.

Examples3

  1. Example 1

    Input
    6 5 1 2 5
    1 3 X
    2 3 X
    3 4 P
    4 5 X
    4 6 Y
    
    Expected output
    1
    
  2. Example 2

    Input
    6 5 1 2 5
    1 3 X
    2 3 X
    3 4 O
    4 5 X
    4 6 Y
    
    Expected output
    0
    
  3. Example 3

    Input
    6 5 1 2 5
    1 3 X
    2 3 X
    3 4 P
    4 5 X
    4 6 G
    
    Expected output
    1