#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 NN vertices, numbered from 00 to N1N-1. The graph has MM edges, numbered from 00 to M1M-1. Each edge connects two different vertices, and edges are bidirectional. More precisely, for each jj from 00 to M1M-1, edge jj connects vertices X[j]X[j] and Y[j]Y[j]. 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 v0,v1,,vkv_0, v_1, \ldots, v_k (for k0k \ge 0), if every pair of consecutive vertices vlv_l and vl+1v_{l+1} (for all ll such that 0l<k0 \le l \lt k) are adjacent, then it is called a path. The path v0,v1,,vkv_0, v_1, \ldots, v_k connects vertices v0v_0 and vkv_k. In the given graph, every pair of vertices is connected by some path.

There are N+1N + 1 colors, numbered from 00 to NN. Color NN is special and is called the Sphinx color. Initially, each vertex has a color: the color of vertex ii (0i<N0 \le i \lt N) is C[i]C[i]. 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, 0C[i]<N0 \le C[i] \lt N (0i<N0 \le i \lt N).

If all vertices on a path v0,v1,,vkv_0, v_1, \ldots, v_k (for k0k \ge 0) have the same color, then the path is monochromatic. That is, it satisfies C[vl]=C[vl+1]C[v_l] = C[v_{l+1}] (for all ll such that 0l<k0 \le l \lt k). Moreover, two vertices pp and qq (0p<N0 \le p \lt N, 0q<N0 \le q \lt N) 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 EE of length NN, and for each ii (0i<N0 \le i \lt N), E[i]E[i] is between 1-1 and NN (inclusive of 1-1 and NN). After recoloring, the color of each vertex ii becomes S[i]S[i], where:

  • If E[i]=1E[i] = -1, then S[i]=C[i]S[i] = C[i], i.e. the original color of vertex ii before recoloring.
  • Otherwise, S[i]=E[i]S[i] = E[i].

Note that you may use the Sphinx color during recoloring.

After setting the color of each vertex ii to S[i]S[i] (0i<N0 \le i \lt N), 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 27502\,750 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)
  • NN: the number of vertices in the graph.
  • XX, YY: two arrays of length MM describing the edges of the graph.
  • The function should return an array GG of length NN, 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)
  • EE: an array of length NN specifying how to recolor the vertices.
  • This function returns the number of monochromatic components after recoloring as specified by EE.
  • This function may be called at most 27502\,750 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, LL is the length of the array GG returned by find_colours, and QQ 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 C=[2,0,0,0]C = [2, 0, 0, 0], 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 11 and 22 both have color 00. Therefore, the path 1,21, 2 is a monochromatic path, so vertices 11 and 22 are in the same monochromatic component.

Vertices 11 and 33 both have color 00. However, since there is no monochromatic path connecting them, they are in different monochromatic components.

In total, there are 33 monochromatic components: vertex sets {0}\{0\}, {1,2}\{1, 2\}, and {3}\{3\}. Therefore, this call returns 33.

Now suppose the function calls perform_experiment as follows:

perform_experiment([0, -1, -1, -1])

This call recolors only vertex 00 to color 00, producing the result shown below.

Now all vertices belong to a single monochromatic component, so this call returns 11. From this, we can infer that vertices 11, 22, and 33 all have color 00.

Suppose the function also calls perform_experiment as follows:

perform_experiment([-1, -1, -1, 2])

This call recolors vertex 33 to color 22, producing the result shown below.

Now there are 22 monochromatic components: vertex sets {0,3}\{0, 3\} and {1,2}\{1, 2\}. Therefore, this call returns 22. From this, we can infer that vertex 00 has color 22.

Then find_colours returns the array [2,0,0,0][2, 0, 0, 0]. Since C=[2,0,0,0]C = [2, 0, 0, 0], you get full score.

There are also many other possible return values, such as [1,2,2,2][1, 2, 2, 2] or [1,2,2,3][1, 2, 2, 3], which can earn 50%50\% of the score.

Constraints

  • 2N2502 \le N \le 250
  • N1MN(N1)2N - 1 \le M \le \frac{N \cdot (N - 1)}{2}
  • For all jj such that 0j<M0 \le j \lt M, 0X[j]<Y[j]<N0 \le X[j] \lt Y[j] \lt N.
  • For all jj and kk such that 0j<k<M0 \le j \lt k \lt M, X[j]X[k]X[j] \neq X[k] or Y[j]Y[k]Y[j] \neq Y[k].
  • Every pair of vertices is connected by some path.
  • For all ii such that 0i<N0 \le i \lt N, 0C[i]<N0 \le C[i] \lt N.
Subtask Score Additional constraints
1 33 N=2N = 2
2 77 N50N \le 50
3 3333 The given graph is a path: M=N1M = N - 1, and vertices jj and j+1j+1 are adjacent (0j<M0 \leq j < M).
4 2121 The given graph is complete: M=N(N1)2M = \frac{N \cdot (N - 1)}{2}, and any two vertices are adjacent.
5 3636 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 GG that is exactly the same as CC (i.e. for all ii such that 0i<N0 \le i \lt N, G[i]=C[i]G[i] = C[i]), 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 50%50\% of the score for that subtask:

  • For all ii such that 0i<N0 \le i \lt N, 0G[i]<N0 \le G[i] \lt N.
  • For all jj such that 0j<M0 \le j \lt M, we have:
    • G[X[j]]=G[Y[j]]G[X[j]] = G[Y[j]] if and only if C[X[j]]=C[Y[j]]C[X[j]] = C[Y[j]].

Translated by ChatGPT 5