Magic Ritual
题号:NC241123
时间限制:C/C++/Rust/Pascal 4秒,其他语言8秒
空间限制:C/C++/Rust/Pascal 1024 M,其他语言2048 M
64bit IO Format: %lld

题目描述

There is an ancient tree with n vertices and n-1 edges. The vertices are numbered through . A magic ritual lasts m days. On the -th day, there is a subset
of edges being chosen, and you can choose to keep the edges in S_i and discard the rest, or you can choose to keep the remaining edges in and discard all edges in S_i. Either way, the vertices in V will be partitioned into several connected components. By the end of the day, the discarded edges will grow back, and the same tree persists the next day.

You wish to compute, for each vertex , if you are able to decide whether keeping the edges in S_i or for each day, what is the maximum possible size of the intersection of the connected components containing v on all m days?
As this is a forbidden ritual, you will not be given the tree T nor the set of edges S_i on each day. Instead, you will only receive the information of the connected components after keeping edges in S_i or respectively for each day. It can be shown that the answer can be uniquely determined given the information.

输入描述:

The first line contains two integers , denoting the size of the tree and the duration of the magic ritual in days, respectively.

Then the information of the m days of the magic ritual follows. The information of the th day of the magic ritual is given as follows:

First, the description of the connected components in the graph is given. The first line consists of an integer , denoting the number of connected components after keeping all edges in S_i and discarding the rest.

Then a_i lines follow, in the -th line an integer k is given, denoting the size of a connected component, then k integers follows, denoting the number of vertices in the connected component. It is guaranteed that the input given by the a_i lines forms a valid decomposition of the vertices.

Then the description of the connected components in the graph is given. A line follows with an integer , denoting the number of connected components after keeping edges in and discarding all edges in S_i.

Then b_i lines follow, in the -th line an integer k is given, denoting the size of a connected component, then k integers follows, denoting the number of vertices in the connected component. It is guaranteed that the input given by the b_i lines forms a valid decomposition of the vertices.

It is guaranteed that there exists at least one tree T and choices of that are consistent with the input.

输出描述:

The output consists of a line containing n integers, denoting the answer for the vertices , respectively.
示例1

输入

复制
6 2
3
4 1 2 3 4
1 5
1 6
4
3 2 5 6
1 1
1 3
1 4
3
4 2 3 5 6
1 1
1 4
4
2 1 2
2 3 4
1 5
1 6

输出

复制
2 3 2 2 3 3

备注:

For the sample test, one valid tree  and choices of S_1, S_2 corresponding to the input, is given as ,  and , as shown in the following graphs, where red edges represent edges in S_1 and S_2, and blue edges represent edges in  and , respectively.





For vertex 1, one optimal way is to keep S_1 and , leading to an intersection set of size 2.

For vertex 2, one optimal way is to keep and S_2, leading to an intersection set of size 3.

For vertex 3, one optimal way is to keep S_1 and S_2, leading to an intersection set of size 2.

For vertex 4, one optimal way is to keep S_1 and , leading to an intersection set of size 2.

For vertex 5, one optimal way is to keep and S_2, leading to an intersection set of size 3.

For vertex 6, one optimal way is to keep and S_2, leading to an intersection set of size 3.