Paint the Roads
Time limit1.5sMemory limit1024 MB
Given a graph with colored roads, decide whether a walk can leave each building with its target color, and output such a walk within 1,000,000 visits if one exists.
- Level
Hard8 of 10
- Topics
- Graph, Implementation
- Solved
- No attempts yet
Problem
A city has buildings and roads. Road connects building and building in both directions. No two roads connect the same pair of buildings. Each road is painted with one of colors, and road has color . The paint is still wet, so the building visited right after traveling along road is painted over with color . Initially, every building has color . ()
Performance artist Eunha wants to paint the buildings using the following method.
- Eunha picks a starting building and goes there.
- Repeat the following step until every building has its desired color.
- From her current building , she moves along a road with color to an adjacent building . She may visit the same building many times. There must exist an with or .
- She paints building with color , regardless of its previous color.
Eunha wants building to have color for every with . Determine whether this is possible. If it is, output one way to do it.
Input
The first line contains the number of buildings , the number of roads , and the number of colors , separated by spaces. (, , )
The second line contains the target colors , separated by spaces. ()
Each of the next lines contains , , and , separated by spaces. (, , and if then )
All input values are integers.
Output
For every with , if building can be painted with color , print YES on the first line. Otherwise print NO.
If you print YES, print the number of buildings Eunha visits on the second line, and the building numbers she visits in order on the third line, separated by spaces. The total number of visits must not exceed . After visiting the buildings in this order, every building must have color . It can be proven that whenever painting is possible, it can be done with at most visits for every valid input.