#P11054. [IOI 2024] 斯芬克斯的谜题
[IOI 2024] 斯芬克斯的谜题
Background
When submitting, please do not include sphinx.h, and add the following at the beginning of your code:
#include <vector>
int perform_experiment(std::vector<int> E);
Do not submit using C++14 (GCC 9).
Problem Description
The Sphinx has prepared a riddle for you. You are given a graph with vertices, numbered from to . The graph has edges, numbered from to . Each edge connects two different vertices, and edges are bidirectional. More precisely, for each from to , edge connects vertices and . There is at most one edge between any two vertices. If two vertices are connected by an edge, they are adjacent.
For a vertex sequence (for ), if every pair of consecutive vertices and (for all such that ) are adjacent, then it is called a path. The path connects vertices and . In the given graph, every pair of vertices is connected by some path.
There are colors, numbered from to . Color is special and is called the Sphinx color. Initially, each vertex has a color: the color of vertex () is . Multiple vertices may have the same color, some colors may not appear on any vertex, and no vertex initially has the Sphinx color. That is, ().
If all vertices on a path (for ) have the same color, then the path is monochromatic. That is, it satisfies (for all such that ). Moreover, two vertices and (, ) are in the same monochromatic component if and only if they are connected by some monochromatic path.
You know the graph structure (the vertices and edges), but you do not know the colors of the vertices. You want to find out the vertex colors by performing recoloring experiments.
In one recoloring experiment, you may recolor any number of vertices. More precisely, in an experiment you provide an array of length , and for each (), is between and (inclusive of and ). After recoloring, the color of each vertex becomes , where:
- If , then , i.e. the original color of vertex before recoloring.
- Otherwise, .
Note that you may use the Sphinx color during recoloring.
After setting the color of each vertex to (), the Sphinx announces the number of monochromatic components in the graph. This new coloring is only valid for this experiment, so after the experiment ends, all vertex colors return to the initial ones.
Your task is to determine the colors of all vertices using at most recoloring experiments. If you correctly determine, for every pair of adjacent vertices, whether they have the same color, you will also receive partial score.
Implementation details
You need to implement the following function:
std::vector<int> find_colours(int N,
std::vector<int> X, std::vector<int> Y)
- : the number of vertices in the graph.
- , : two arrays of length describing the edges of the graph.
- The function should return an array of length , representing the colors of the vertices.
- For each test case, this function is called exactly once.
This function can perform recoloring experiments by calling:
int perform_experiment(std::vector<int> E)
- : an array of length specifying how to recolor the vertices.
- This function returns the number of monochromatic components after recoloring as specified by .
- This function may be called at most times.
The grader is not adaptive. That is, the vertex colors are fixed before find_colours is called.
Input Format
The sample grader reads input in the following format:
N M
C[0] C[1] ... C[N-1]
X[0] Y[0]
X[1] Y[1]
...
X[M-1] Y[M-1]
Output Format
The sample grader prints your answer in the following format:
L Q
G[0] G[1] ... G[L-1]
Here, is the length of the array returned by find_colours, and is the number of calls to perform_experiment.
4 4
2 0 0 0
0 1
1 2
0 2
0 3
4 3
2 0 0 0
Hint
Consider the following function call:
find_colours(4, [0, 1, 0, 0], [1, 2, 2, 3])
In this example, suppose the (hidden) vertex colors are , as shown in the figure below. The vertex colors are also shown as numbers in the labels at the upper-right corner of the vertices.

Suppose the function calls perform_experiment as follows:
perform_experiment([-1, -1, -1, -1])
This call does not recolor any vertex, so all vertices keep their original colors.
Vertices and both have color . Therefore, the path is a monochromatic path, so vertices and are in the same monochromatic component.
Vertices and both have color . However, since there is no monochromatic path connecting them, they are in different monochromatic components.
In total, there are monochromatic components: vertex sets , , and . Therefore, this call returns .
Now suppose the function calls perform_experiment as follows:
perform_experiment([0, -1, -1, -1])
This call recolors only vertex to color , producing the result shown below.

Now all vertices belong to a single monochromatic component, so this call returns . From this, we can infer that vertices , , and all have color .
Suppose the function also calls perform_experiment as follows:
perform_experiment([-1, -1, -1, 2])
This call recolors vertex to color , producing the result shown below.

Now there are monochromatic components: vertex sets and . Therefore, this call returns . From this, we can infer that vertex has color .
Then find_colours returns the array . Since , you get full score.
There are also many other possible return values, such as or , which can earn of the score.
Constraints
- For all such that , .
- For all and such that , or .
- Every pair of vertices is connected by some path.
- For all such that , .
| Subtask | Score | Additional constraints |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | The given graph is a path: , and vertices and are adjacent (). | |
| 4 | The given graph is complete: , and any two vertices are adjacent. | |
| 5 | No additional constraints. |
In each subtask, if your program correctly determines, for every pair of adjacent vertices, whether they have the same color, you will also receive partial score.
More precisely, if for all test cases find_colours returns an array that is exactly the same as (i.e. for all such that , ), you will receive the full score for that subtask. Otherwise, if for all test cases in a subtask the following conditions hold, you will receive of the score for that subtask:
- For all such that , .
- For all such that , we have:
- if and only if .
Translated by ChatGPT 5