Changing Colors

Time limit: 0.1s Memory limit: 256MB Input: Output:

Your friend has a connected graph with NN vertices and N1N-1 edges, meaning that the graph is a tree -- there is a unique path between any two nodes.
They secretly choose two distinct favorite nodes, AA and BB, such that the distance between them is at least 22.
The vertices are numbered from 11 to NN and the edges are numbered from 11 to N1N - 1.

You play a guessing game to discover which two nodes your friend picked. In a single question, you can color each edge of the graph either black or white. Your friend then responds with a number: the number of color changes along the unique path from AA to BB.

Formally, let the path from AA to BB be A=v0,v1,v2,,vk=BA = v_0, v_1, v_2, \ldots, v_k = B, with k2k \geq 2. The response is the number of inner nodes viv_i (1i<k1 \le i < k) such that the colors of edges (vi1,vi)(v_{i-1},v_i) and (vi,vi+1)(v_i,v_{i+1}) are different.

You may ask up to 3131 questions. If you ask more, your friend will get bored and stop playing. Your task is to determine the hidden nodes AA and BB using at most 3131 questions.

Implementation

Your task is to implement the following function:

std::pair<int, int> find_nodes(int N, const std::vector<std::pair<int, int>>& edges);
  • int N: the number of vertices in the graph.
  • edges: a vector of N1N-1 pairs, where edges[i] = \{X_i, Y_i\} means that edge ii connects vertex XiX_i to YiY_i.
  • returns a pair of integers (A, B) — the two favorite nodes your friend picked. The order does not matter.

Interaction

You may ask up to 31 questions using the following function:

int query(const std::vector<int>& colors);
  • The colors vector must be of length N1N-1, where colors[i] is the color of edge i+1i+1: 0 for white and 1 for black.
  • The function returns the number of color changes along the unique path from AA to BB.
  • If your function exceeds 31 queries or provides malformed input, query will return -1.
  • You must #include "colors.h"

Constraints

  • 3N40 0003 \le N \le 40\ 000
  • 1XiYiN1 \le X_i \ne Y_i \le N for each 1iN11 \le i \le N - 1
  • The edges form a tree
  • The hidden nodes AA and BB are distinct and have distance at least 22

Scoring

Your program will be tested against several test cases grouped in subtasks.
In order to obtain the score of a subtask, your program needs to correctly solve all of its test cases.

Subtask Points Constraints
0 1 Examples
1 4 N32N \le 32
2 7 N150N \le 150
3 12 The graph is a line, and dist(A,B)=2\text{dist}(A, B) = 2
4 22 The graph is a line (exactly two nodes of degree 1)
5 26 The graph is a star (N1N - 1 nodes of degree 1)
6 29 No additional constraints

Example

N = 7
edges = {
  {3, 4},
  {1, 6},
  {1, 5},
  {7, 2},
  {7, 3},
  {4, 1}
}

Suppose the following calls are made:

query({0, 0, 0, 0, 1, 1})  3
query({0, 1, 1, 0, 0, 0})  1
query({0, 1, 0, 0, 0, 0})  0

Then your function should return:

return {5, 7};

Explanation

  • After the first query, the only possible pairs of hidden nodes are (1,2)(1, 2), (5,7)(5, 7) and (6,7)(6, 7), as the secret path has 33 color changes.
  • After the second query, only (5,7)(5, 7) and (6,7)(6, 7) are possible: (1,2)(1, 2) is excluded.
  • After the third query, only (5,7)(5, 7) remains (the path between 66 and 77 has a color change).

Log in or sign up to be able to send submissions!