시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 1024 MB122231718.681%

문제

You are given a rooted binary tree consisting of $N$ vertices. The vertices are numbered from $1$ to $N$, the root is the vertex number $1$. Each of the other vertices has a single parent in the tree. The tree is binary, i.e. each vertex can be a parent of at most two other vertices.

One of the vertices is special. You are trying to guess it. You can ask the questions of the following kind: "Is the special vertex in the subtree of vertex $x$"? A node $y$ is in the subtree of vertex $x$ if and only if the shortest path between $y$ and $1$ goes through vertex $x$. Note that vertex $x$ is also in its own subtree.

You are allowed to ask this question at most $35$ times. After that you should report your guess.

구현

You should implement the following procedure:

int solve(int N, std::vector < int > p)
  • $N$: the number of vertices
  • $p$ contains exactly $N - 1$ elements that describe the tree: the vertex $p[i]$ (where $1 ≤ p[i] ≤ i + 1$) is the parent of the vertex $i + 2$ for each $0 ≤ i ≤ N - 2$
  • No element in $p$ appears more than twice
  • This procedure should return the number of the special vertex
  • This procedure is called exactly once

The above procedure can make calls to the following procedure:

int ask(int x)
  • $x$: the number of the vertex
  • $1 ≤ x ≤ N$
  • returns $1$ if the special vertex is in the subtree of $x$ and $0$ otherwise

예제

Consider the following call:

solve(5, [1, 1, 2, 4])

The tree consists of the edges $(1,2)$, $(1,3)$, $(2,4)$ and $(4,5)$.

Your program made a call

ask(4)

which returned $1$. After that your program made a call

ask(5)

which returned $0$.

Your program concluded that vertex $4$ is special and returned $4$.

제한

  • $2 ≤ N ≤ 100\, 000$

서브태스크

번호배점제한
120

$N ≤ 35$

230

$p[i] = i + 1$ for each $0 ≤ i ≤ N - 2$

315

$p[i] = \lfloor i/2 \rfloor + 1$ for each $0 ≤ i ≤ N - 2$

435

No additional constraints.

샘플 그레이더

The sample grader reads the input in the following format:

  • line $1$: $N$
  • line $2$: $p[0]$, $p[1]$, $\dots$, $p[N - 2]$

The sample grader outputs each question in the following format:

  • line $1$: ? $x$

The sample grader reads each answer in the following format:

  • line $1$: $y$

The sample grader outputs the guess in the following format:

  • line $1$: ! $x$

첨부

제출할 수 있는 언어

C++17, C++20, C++17 (Clang), C++20 (Clang)

채점 및 기타 정보

  • 예제는 채점하지 않는다.