CF BUDDY
← Problems·

1146C · Tree Diameter

1700 · bitmasks, graphs, interactive

Problem: There is a weighted tree with nn nodes and n1n-1 edges. The nodes are conveniently labeled from 11 to nn. The weights are positive integers at most 100100. Define the distance between two nodes to be the sum of edges on the unique path between the nodes. You would like to find the diameter of the tree. Diameter is the maximum distance between a pair of nodes.

Unfortunately, the tree isn't given to you, but you can ask some questions about it. In one question, you can specify two nonempty disjoint sets of nodes pp and qq, and the judge will return the maximum distance between a node in pp and a node in qq. In the words, maximum distance between xx and yy, where xpx \in p and yqy \in q. After asking not more than 99 questions, you must report the maximum distance between any pair of nodes.

Note: In the first example, the first tree looks as follows:

In the first question, we have p=1p = {1}, and q=2,3,4,5q = {2, 3, 4, 5}. The maximum distance between a node in pp and a node in qq is 99 (the distance between nodes 11 and 55).

The second tree is a tree with two nodes with an edge with weight 9999 between them.

Sample Cases

Case 1

Input

2
5
9
6
10
9
10
2
99

Output

1 4 1 2 3 4 5
1 4 2 3 4 5 1
1 4 3 4 5 1 2
1 4 4 5 1 2 3
1 4 5 1 2 3 4
-1 10
1 1 1 2
-1 99

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback