Your friend has a connected graph with vertices and edges, meaning that the graph is a tree -- there is a unique path between any two nodes.
They secretly choose two distinct favorite nodes, and , such that the distance between them is at least .
The vertices are numbered from to and the edges are numbered from to .
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 to .
Formally, let the path from to be , with . The response is the number of inner nodes () such that the colors of edges and are different.
You may ask up to questions. If you ask more, your friend will get bored and stop playing. Your task is to determine the hidden nodes and using at most 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 pairs, whereedges[i] = \{X_i, Y_i\}
means that edge connects vertex to .- 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 , wherecolors[i]
is the color of edge :0
for white and1
for black. - The function returns the number of color changes along the unique path from to .
- If your function exceeds 31 queries or provides malformed input,
query
will return-1
. - You must
#include "colors.h"
Constraints
- for each
- The edges form a tree
- The hidden nodes and are distinct and have distance at least
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 | |
2 | 7 | |
3 | 12 | The graph is a line, and |
4 | 22 | The graph is a line (exactly two nodes of degree 1) |
5 | 26 | The graph is a star ( 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 , and , as the secret path has color changes.
- After the second query, only and are possible: is excluded.
- After the third query, only remains (the path between and has a color change).