#P3233. [HNOI2014] 世界树

    ID: 2282 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp2014倍增湖南最近公共祖先,LCA虚树

[HNOI2014] 世界树

Description

The World Tree is an unimaginably huge tree whose sprawling branches form the entire world. Here live various races and beings. They all worship the goddess Alison, who stands for absolute justice and fairness. In their creed, fairness is the fundamental cornerstone that allows the World Tree to thrive and keep running.

The shape of the World Tree can be modeled mathematically: there are nn races in the World Tree, numbered from 11 to nn, each living in a settlement also numbered from 11 to nn, and a race’s number equals its settlement’s number. Some settlements are connected by bidirectional roads, each of length 11. It is guaranteed that the connections form a tree, that is, every settlement is reachable from every other, and there are no cycles. The distance between two settlements is defined as the length of the unique path connecting them. For example, if there is a road between settlements aa and bb, and a road between bb and cc, then since each road has length 11 and cycles are impossible, the distance between aa and cc is 22.

For fairness, in year ii, the king of the World Tree authorizes mim_i settlements as temporary offices. For a race xx (where xx is the race’s number), if the nearest temporary office to race xx is at settlement yy (where yy is the settlement number of the office), then race xx accepts the administration of the office at yy (if multiple temporary offices are at the same minimum distance to that settlement, then yy is the office with the smallest number among them).

Now the king wants to know, over qq years, after the authorizations in each year are completed, how many races each temporary office will administer that year (the settlement where an office is located also accepts that office’s administration). This task is handed to you, the wise primate: the programmer. Please help the king complete this task.

Input Format

The first line contains a positive integer nn, the number of races in the World Tree. The next n1n-1 lines each contain two positive integers x,yx, y, indicating that there is a bidirectional road of length 11 between settlements xx and yy. The next line contains a positive integer qq, the number of years the king queries. Then follow qq blocks, each consisting of two lines: in the ii-th block, the first line contains one positive integer mim_i, the number of temporary offices authorized in year ii. The second line contains mim_i positive integers h1,h2,,hmih_1, h_2, \ldots, h_{m_i}, the settlement numbers authorized as temporary offices (guaranteed to be all distinct).

Output Format

Output qq lines. In the ii-th line, output mim_i integers, where the jj-th number (j=1,2,,mij = 1, 2, \ldots, m_i) is the number of races administered by the temporary office at settlement hjh_j authorized in year ii.

10
2 1
3 2
4 3
5 4
6 1
7 3
8 3
9 4
10 1
5
2
6 1
5
2 7 3 6 9
1
8
4
8 7 10 3
5
2 9 3 5 8
1 9   
3 1 4 1 1   
10  
1 1 3 5   
4 1 3 1 1

Hint

For 100%100\% of the testdata, N300000N\leq 300000, q300000q\leq 300000, i=1qmi300000\sum^q_{i=1}m_i \leq 300000.

Translated by ChatGPT 5