atcoder#ARC142C. [ARC142C] Tree Queries

[ARC142C] Tree Queries

Score : 500500 points

Problem Statement

There is a tree with NN vertices, numbered 1,,N1, \ldots, N. For each pair of integers u,v(1u,vN)u,v\, (1 \leq u,v \leq N), the distance du,vd_{u,v} between Vertices u,vu, v is defined as the following.

  • The number of edges contained in the shortest path connecting Vertices uu and vv.

You are allowed to ask between 00 and 2N2N questions (inclusive) in the following form.

  • Ask the distance du,vd_{u,v} between Vertices u,vu,v for integers u,vu,v of your choice such that 1u,vN1\leq u,v \leq N and u+v>3u+v>3.

Find the distance d1,2d_{1,2} between Vertices 1,21,2.

Constraints

  • 3N1003 \leq N \leq 100
  • NN is an integer.
  • The tree is determined before the start of the interaction between your program and the judge.

Input and Output

This is an interactive task, in which your program and the judge interact via input and output.

First, your program is given a positive integer NN from Standard Input:

N

Then, you get to ask questions. A question should be printed in the following format (with a newline at the end):

? u v

If the question is valid, the response du,vd_{u,v} is given from Standard Input:

d_{u,v}

If the question is judged invalid because, for example, it is malformed or you have asked too many questions, you get -1 instead of the response:

-1

At this point, your submission is already judged incorrect. The judge's program then terminates; yours should too, desirably.

When you find the answer d1,2d_{1,2}, print it to Standard Output in the following format (with a newline at the end):

! d_{1,2}

Notices

  • Flush Standard Output after each output. Otherwise, you might get the TLE verdict.
  • After printing the answer (or receiving -1), immediately terminate the program normally. Otherwise, the verdict would be indeterminate.
  • The verdict for the case of a malformed output would be indeterminate.
  • Specifically, note that too many newlines would also be seen as a malformed output.

Sample Input and Output

The following is a sample interaction for the tree shown in this image.

Input Output Description
3 Interaction starts with the integer NN.
? 1 3 As the 11-st question, you ask the distance between Vertices 11 and 33.
1 The distance between Vertices 1,31,3 is returned.
? 2 2 As the 22-nd question, you ask the distance between Vertices 22 and 22.
0 The distance between Vertices 2,22,2 is returned.
? 2 3 As the 33-rd question, you ask the distance between Vertices 22 and 33.
1 The distance between Vertices 2,32,3 is returned.
? 3 1 As the 44-th question, you ask the distance between Vertices 33 and 11.
1 The distance between Vertices 3,13,1 is returned.
? 3 2 As the 55-th question, you ask the distance between Vertices 33 and 22.
1 The distance between Vertices 3,23,2 is returned.
? 2 2 As the 66-th question, you ask the distance between Vertices 22 and 22.
0 The distance between Vertices 2,22,2 is returned.
! 2 You guess the distance between Vertices 1,21,2 and terminate. The actual distance between them is 22, so you would get the AC verdict.