Sphinx's Riddle
문제
The Great Sphinx has a riddle for you. You are given a graph on vertices. The vertices are numbered from to . There are edges in the graph, numbered from to . Each edge connects a pair of distinct vertices and is bidirectional. Specifically, for each from to (inclusive) edge connects vertices and . There is at most one edge connecting any pair of vertices. Two vertices are called adjacent if they are connected by an edge.
A sequence of vertices (for k ≥ 0) is called a path if each two consecutive vertices and (for each such that 0 ≤ l < k) are adjacent. We say that a path connects vertices and . In the graph given to you, each pair of vertices is connected by some path.
There are colours, numbered from to . Colour is special and is called the Sphinx's colour. Each vertex is assigned a colour. Specifically, vertex (0 ≤ i < N) has colour . Multiple vertices may have the same colour, and there might be colours not assigned to any vertex. No vertex has the Sphinx's colour, that is, 0 ≤ C[i] < N (0 ≤ i < N).
A path (for k ≥ 0) is called monochromatic if all of its vertices have the same colour, i.e. (for each such that 0 ≤ l < k). Additionally, we say that vertices and (0 ≤ p < N, 0 ≤ q < N) are in the same monochromatic component if and only if they are connected by a monochromatic path.
You know the vertices and edges, but you do not know which colour each vertex has. You want to find out the colours of the vertices, by performing recolouring experiments.
In a recolouring experiment, you may recolour arbitrarily many vertices. Specifically, to perform a recolouring experiment you first choose an array of size , where for each (0 ≤ i < N), is between and inclusive. Then, the colour of each vertex becomes , where the value of is:
- , that is, the original colour of , if , or
- , otherwise.
Note that this means that you can use the Sphinx's colour in your recolouring.
Finally, the Great Sphinx announces the number of monochromatic components in the graph, after setting the colour of each vertex to (0 ≤ i < N). The new colouring is applied only for this particular recolouring experiment, so the colours of all vertices return to the original ones after the experiment finishes.
Your task is to identify the colours of the vertices in the graph by performing at most recolouring experiments. You may also receive a partial score if you correctly determine for every pair of adjacent vertices, whether they have the same colour.