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

#GL0000398. Phủ cây bằng ít đường đi nhất (Minimum Good Vertical Path Cover)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Phủ cây bằng ít đường đi nhất (Minimum Good Vertical Path Cover)

Minimum Good Vertical Path Cover

Source: Codeforces

Version: Phuoc Hung OJ Extended

Mode: streaming interactive problem.

Problem Statement

A rooted tree has nn vertices, root 1, and a value au≥2a_u\ge2 on each vertex. Every parent index is smaller than each of its child indices.

A vertical path follows parent-to-child edges without branching. It is good if the GCD of all values on this path is greater than 1. Let f(S(u))f(S(u)) be the minimum number of pairwise vertex-disjoint good vertical paths that cover all nodes in the subtree rooted at uu.

You must output f(S(n)),f(S(n−1)),…,f(S(1))f(S(n)),f(S(n-1)),\ldots,f(S(1)) in this order.

Interaction

First, read nn. For each u=n,n−1,…,1u=n,n-1,\ldots,1, the judge sends a_u k c_1 ... c_k, where kk is the number of children, and each ci>uc_i>u is a direct child of uu. Immediately output f(S(u))f(S(u)) and flush. The judge sends the next vertex's information only after validating your current answer. All outputs must be correct.

Each PHOJ .in file stores one complete hidden tree. The interactor streams the vertex records one at a time; contestants must never read the hidden file directly. Unlike the original multi-case statement, one test case is used per PHOJ file.

Subtasks

  • Subtask 1 (20 points): 2≤n≤202\le n\le20, 2≤au≤10182\le a_u\le10^{18}, valid rooted tree with increasing indices.
  • Subtask 2 (30 points): 2≤n≤5 0002\le n\le5\,000, 2≤au≤10182\le a_u\le10^{18}, valid rooted tree with increasing indices.
  • Subtask 3 (50 points): 2≤n≤200 0002\le n\le200\,000, 2≤au≤10182\le a_u\le10^{18}, valid rooted tree with increasing indices.

Example interaction

Judge: 3
Judge: 6 0
Player: 1
Judge: 10 0
Player: 1
Judge: 15 2 2 3
Player: 2

Explanation: Nodes 2 and 3 individually require one path. In the subtree of node 1, paths [1,2][1,2] (GCD 5) and [3][3] (GCD 6) cover all nodes. One vertical path cannot cover both children of node 1, so the optimal number is 2.