View problem - Sphinx's Riddle (IOI24_sphinx)

Time limitMemory limit# of submissions# of submitted usersSolved #Accepted user ratio
1500 ms2048 MiB216301136.67%

The Great Sphinx has a riddle for you. You are given a graph on NN vertices. The vertices are numbered from 00 to N1N - 1. There are MM edges in the graph, numbered from 00 to M1M-1. Each edge connects a pair of distinct vertices and is bidirectional. Specifically, for each jj from 00 to M1M - 1 (inclusive) edge jj connects vertices X[j]X[j] and Y[j]Y[j]. 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 v0,v1,,vkv_0, v_1, \ldots, v_k (for k0k \ge 0) is called a path if each two consecutive vertices vlv_l and vl+1v_{l+1} (for each ll such that 0l<k0 \le l \lt k) are adjacent. We say that a path v0,v1,,vkv_0, v_1, \ldots, v_k connects vertices v0v_0 and vkv_k. In the graph given to you, each pair of vertices is connected by some path.

There are N+1N + 1 colours, numbered from 00 to NN. Colour NN is special and is called the Sphinx's colour. Each vertex is assigned a colour. Specifically, vertex ii (0i<N0 \le i \lt N) has colour C[i]C[i]. 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, 0C[i]<N0 \le C[i] \lt N (0i<N0 \le i \lt N).

A path v0,v1,,vkv_0, v_1, \ldots, v_k (for k0k \ge 0) is called monochromatic if all of its vertices have the same colour, i.e. C[vl]=C[vl+1]C[v_l] = C[v_{l+1}] (for each ll such that 0l<k0 \le l \lt k). Additionally, we say that 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 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 EE of size NN, where for each ii (0i<N0 \le i \lt N), E[i]E[i] is between 1-1 and NN inclusive. Then, the colour of each vertex ii becomes S[i]S[i], where the value of S[i]S[i] is:

  • C[i]C[i], that is, the original colour of ii, if E[i]=1E[i] = -1, or
  • E[i]E[i], 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 ii to S[i]S[i] (0i<N0 \le i \lt 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 2,7502,750 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.

Implementation Details

You should implement the following procedure.

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: arrays of length MM describing the edges.
  • This procedure should return an array GG of length NN, representing the colours of vertices in the graph.
  • This procedure is called exactly once for each test case.

The above procedure can make calls to the following procedure to perform recolouring experiments:

int perform_experiment(std::vector<int> E)
  • EE: an array of length NN specifying how vertices should be recoloured.
  • This procedure returns the number of monochromatic components after recolouring the vertices according to EE.
  • This procedure can be called at most 2,7502,750 times.

The grader is not adaptive, that is, the colours of the vertices are fixed before a call to find_colours is made.

Constraints

  • 2N2502 \le N \le 250
  • N1MN(N1)2N - 1 \le M \le \frac{N \cdot (N - 1)}{2}
  • 0X[j]<Y[j]<N0 \le X[j] \lt Y[j] \lt N for each jj such that 0j<M0 \le j \lt M.
  • X[j]X[k]X[j] \neq X[k] or Y[j]Y[k]Y[j] \neq Y[k] for each jj and kk such that 0j<k<M0 \le j \lt k \lt M.
  • Each pair of vertices is connected by some path.
  • 0C[i]<N0 \le C[i] \lt N for each ii such that 0i<N0 \le i \lt N.

Subtasks

Subtask Score Additional Constraints
1 33 N=2N = 2
2 77 N50N \le 50
3 3333 The 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 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, you can obtain a partial score if your program determines correctly for every pair of adjacent vertices whether they have the same colour.

More precisely, you get the whole score of a subtask if in all of its test cases, the array GG returned by find_colours is exactly the same as array CC (i.e. G[i]=C[i]G[i] = C[i] for all ii such that 0i<N0 \le i \lt N). Otherwise, you get 5050% of the score for a subtask if the following conditions hold in all of its test cases:

  • 0G[i]<N0 \le G[i] \lt N for each ii such that 0i<N0 \le i \lt N;
  • For each jj such that 0j<M0 \le j \lt M:
    • 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]].

Example

Consider the following call.

find_colours(4, [0, 1, 0, 0], [1, 2, 2, 3])

For this example, suppose that the (hidden) colours of the vertices are given by C=[2,0,0,0]C = [2, 0, 0, 0]. This scenario is shown in the following figure. Colours are additionally represented by numbers on white labels attached to each vertex.

example.png

The procedure may call perform_experiment as follows.

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

In this call, no vertex is recoloured, as all vertices keep their original colours.

Consider vertex 11 and vertex 22. They both have colour 00 and the path 1,21, 2 is a monochromatic path. As a result, vertices 11 and 22 are in the same monochromatic component.

Consider vertex 11 and vertex 33. Even though both of them have colour 00, they are in different monochromatic components as there is no monochromatic path connecting them.

Overall, there are 33 monochromatic components, with vertices 0{0}, 1,2{1, 2}, and 3{3}. Thus, this call returns 33.

Now the procedure may call perform_experiment as follows.

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

In this call, only vertex 00 is recoloured to colour 00, which results in the colouring shown in the following figure.

example.png

This call returns 11, as all the vertices belong to the same monochromatic component. We can now deduce that vertices 11, 22, and 33 have colour 00.

The procedure may then call perform_experiment as follows.

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

In this call, vertex 33 is recoloured to colour 22, which results in the colouring shown in the following figure.

example.png

This call returns 22, as there are 22 monochromatic components, with vertices 0,3{0, 3} and 1,2{1, 2} respectively. We can deduce that vertex 00 has colour 22.

The procedure find_colours then returns the array [2,0,0,0][2, 0, 0, 0]. Since C=[2,0,0,0]C = [2, 0, 0, 0], full score is given.

Note that there are also multiple return values, for which 5050% of the score would be given, for example [1,2,2,2][1, 2, 2, 2] or [1,2,2,3][1, 2, 2, 3].

Sample Grader

Input 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:

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.

Attachments
File nameSize
sphinx.zip2.91 KiB