Painted Corridors
Time limit5sMemory limit512 MB
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 (), (), , , and (). Here is the number of junctions, is the number of corridors, and , , and are the starting junctions of the red, blue, and yellow robots. Junctions are numbered 1 through . Each of the next lines contains two integers and () and a character , which is one of R, O, Y, G, B, P, or X. This means there is a corridor between junctions and , and 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.