PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000401. Tìm cặp đỉnh có GCD đường đi lớn nhất (Maximum-GCD Path Pair)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tìm cặp đỉnh có GCD đường đi lớn nhất (Maximum-GCD Path Pair)

Maximum-GCD Path Pair

Source: Codeforces

Version: Phuoc Hung OJ Extended

Mode: interactive.

Problem Statement

The structure of an undirected tree with nn vertices is public, but the positive edge weights wew_e are hidden. For distinct vertices u,vu,v, define Dist⁡(u,v)\operatorname{Dist}(u,v) as the GCD of weights along their unique path.

Find two distinct vertices a,ba,b maximizing Dist⁡(a,b)\operatorname{Dist}(a,b) over all vertex pairs.

You may ask at most 12 queries of the form ? k v_1 ... v_k, where 2≤k≤n2\le k\le n and the listed vertices are distinct. The judge returns the maximum Dist⁡(vi,vj)\operatorname{Dist}(v_i,v_j) over all pairs in the queried set. Finish by printing ! a b, which does not count as a query. Flush every question.

PHOJ hidden input

The test file contains nn followed by n−1n-1 lines u v w. The interactor reveals only nn and all endpoint pairs u v, not the weights. The judge answers queries using the hidden edge weights.

Subtasks

  • Subtask 1 (20 points): 2≤n≤202\le n\le20, 1≤we≤1091\le w_e\le10^9, tree, at most 12 queries.
  • Subtask 2 (30 points): 2≤n≤2002\le n\le200, 1≤we≤1091\le w_e\le10^9, tree, at most 12 queries.
  • Subtask 3 (50 points): 2≤n≤1 0002\le n\le1\,000, 1≤we≤1091\le w_e\le10^9, tree, at most 12 queries.

Example interaction

Suppose the hidden weighted edges are (1,2,6),(2,3,15),(1,4,10)(1,2,6),(2,3,15),(1,4,10).

Judge: 4
Judge: 1 2
Judge: 1 4
Judge: 2 3
Player: ? 4 1 2 3 4
Judge: 15
Player: ? 2 1 2
Judge: 6
Player: ? 3 1 2 3
Judge: 15
Player: ! 2 3

Explanation: The single edge (2,3) gives path GCD 15. A multi-edge path cannot have GCD greater than the maximum individual edge weight, which is 15. Thus this pair is optimal.