QOJ.ac

QOJ

时间限制: 2 s 内存限制: 512 MB 总分: 100

#14274. World Tree

统计

The World Tree is an incredibly massive tree, and its extending branches form the entire world. Various races and creatures live here, all sharing a common faith in the absolutely just and fair goddess Alison. In their creed, fairness is the fundamental cornerstone that allows the World Tree to thrive and operate continuously.

The structure of the World Tree can be described by a mathematical model: there are $n$ races in the World Tree, numbered from $1$ to $n$, living in settlements numbered from $1$ to $n$, respectively. The race number is the same as the settlement number. Some settlements are connected by bidirectional roads of length $1$. It is guaranteed that the connections form a tree structure, meaning all settlements are reachable from each other, and there are no cycles. The distance between two settlements is defined as the length of the path connecting them; for example, if there is a road between settlement $a$ and $b$, and a road between $b$ and $c$, the distance between $a$ and $c$ is $2$, because each road has a length of $1$ and no cycles can exist.

For the sake of fairness, in year $i$, the King of the World Tree needs to authorize $m[i]$ settlements to serve as temporary council offices. For a certain race $x$ (where $x$ is the race number), if the nearest temporary council office is $y$ (where $y$ is the number of the settlement where the office is located), then race $x$ will be under the jurisdiction of office $y$ (if there are multiple temporary council offices at the same distance from this settlement, $y$ is the one with the smallest office number).

Now the King wants to know, over a period of $q$ years, how many races each temporary council office will manage after the authorizations are completed each year (the settlement where the office is located is also managed by that office).

This task is now handed to you, the primate known for wisdom: the programmer. Please help the King complete this task.

Input

The input file is named worldtree.in.

The first line contains a positive integer $n$, representing the number of races in the World Tree.

The next $n-1$ lines each contain two positive integers $x, y$, representing a bidirectional road of length $1$ between settlement $x$ and settlement $y$.

The next line contains a positive integer $q$, representing the number of years the King asks about.

The next $q$ blocks each consist of two lines: The first line of the $i$-th block contains a positive integer $m[i]$, representing the number of temporary council offices authorized in year $i$. The second line of the $i$-th block contains $m[i]$ positive integers $h[1], h[2], \dots, h[m[i]]$, representing the settlement numbers authorized as temporary council offices (guaranteed to be distinct).

Output

The output file is named worldtree.out.

The output contains $q$ lines. The $i$-th line contains $m[i]$ integers, where the $j$-th ($j=1, 2, \dots, m[i]$) number represents the number of races managed by the temporary council office at settlement $h[j]$ in year $i$.

Examples

Input 1

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

Output 1

1 9
3 1 4 1 1
10
1 1 3 5
4 1 3 1 1

Note

The tree structure is shown in the figure:

For the query in the second year: Query: $2, 7, 3, 6, 9$ The number of vertices they manage are: $3, 1, 4, 1, 1$ (points managed by the same office are colored with the same color).

Constraints

For $30\%$ of the data: $n \le 1000, q \le 1000, m[1]+m[2]+\dots+m[q] \le 10000$; For $60\%$ of the data: $n \le 100000, q \le 100000, m[1]+m[2]+\dots+m[q] \le 100000$; For $100\%$ of the data: $n \le 300000, q \le 300000, m[1]+m[2]+\dots+m[q] \le 300000$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.