#P6805. [CEOI 2020] 春季大扫除

[CEOI 2020] 春季大扫除

Description

Spring cleaning might be one of the most boring things in our lives. However, for Flóra and her mother, this year's spring cleaning is much more interesting, because they found a tree-shaped map covered in dust under the carpet.

This tree has NN nodes, numbered from 11 to NN, connected by N1N-1 edges. Too much dust has accumulated on these edges, so Flóra's mother plans to clean the tree.

The cleaning process works as follows: each time, Flóra's mother chooses two leaf nodes in the tree (a leaf node is defined as a node that is directly connected to exactly one other node), and cleans all edges on the path between these two leaves. If there are dd edges on the path, then the cost of this cleaning operation is dd. When all edges in the tree have been cleaned, the cleaning process is finished. The total cost is the sum of the costs of all cleaning operations.

Because she wants to protect the leaf nodes of the tree, for each leaf node, she will choose it at most once.

Flóra thinks the original tree is too simple, so she decides to modify the original tree a bit. In the ii-th modification, she adds DiD_i leaf nodes to the original tree. Specifically, she chooses a node in the original tree and connects it to a new leaf node with an edge. Note that during the process of adding new leaf nodes, some of the original nodes will no longer be leaf nodes.

Now you need to help Flóra find the minimum cost to clean the modified tree.

Input Format

The first line contains two integers N,QN,Q, representing the number of nodes in the original tree and the number of modifications Flóra will perform on the original tree.

The next N1N-1 lines each contain two integers u,vu,v, indicating that there is an edge between nodes uu and vv in the original tree.

The next QQ lines describe the modifications. Each line starts with an integer DiD_i, meaning that Flóra will add DiD_i leaf nodes in the ii-th modification. Then follow DiD_i integers; the jj-th integer aja_j means that Flóra will add one leaf node attached to node aja_j. Multiple leaf nodes may be added to the same node.

Each modification is independent. That is, after finishing one modification, Flóra starts again from the original tree for the next modification.

Output Format

For each modification, output one integer indicating the minimum cost to clean the modified tree.

In particular, if the tree cannot be completely cleaned, output 1-1.

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

Hint

Sample Explanation

The following shows the tree after the second modification:

One optimal cleaning plan is to clean all edges on the three paths 161-6, A7A-7, and B3B-3.

Subtasks

All test cases satisfy: 3N1053 \leq N \leq 10^5, 1Q1051 \leq Q \leq 10^5, Di105\sum D_i \leq 10^5, 1u,vN1 \leq u,v \leq N, 1ajN1 \leq a_j \leq N.

The constraints for each subtask are as follows:

Subtask ID Points Constraints
11 00 Sample
22 99 Q=1Q=1, for all i[2,N]i \in [2,N], there is an edge connecting 11 and ii, and Flóra will not add leaf nodes to node 11
33 Q=1Q=1, for all i[1,N)i \in [1,N), there is an edge between ii and i+1i+1, and Flóra will not add leaf nodes to node 11 or node NN
44 1616 N2×104N \leq 2 \times 10^4, Q300Q \leq 300
55 1919 The original tree is a perfect binary tree rooted at node 11, i.e., every non-leaf node has exactly two children, and the distance from each leaf to the root is the same
66 1717 For all i[1,Q]i \in [1,Q], Di=1D_i=1
77 3030 No special constraints

Translated by ChatGPT 5