#P1403B. Spring cleaning
Spring cleaning
Description
Spring cleanings are probably the most boring parts of our lives, except this year, when Flóra and her mother found a dusty old tree graph under the carpet.
This tree has nodes (numbered from to ), connected by edges. The edges gathered too much dust, so Flóra's mom decided to clean them.
Cleaning the edges of an arbitrary tree is done by repeating the following process: She chooses 2 different leaves (a node is a leaf if it is connected to exactly one other node by an edge), and cleans every edge lying on the shortest path between them. If this path has edges, then the cost of cleaning this path is .
She doesn't want to harm the leaves of the tree, so she chooses every one of them at most once. A tree is cleaned when all of its edges are cleaned. The cost of this is the sum of costs for all cleaned paths.
Flóra thinks the tree they found is too small and simple, so she imagines variations of it. In the -th variation, she adds a total of extra leaves to the original tree: for each new leaf, she chooses a node from the original tree, and connects that node with the new leaf by an edge. Note that some nodes may stop being leaves during this step.
For all these variations, we are interested in the minimum cost that is required to clean the tree.
The first line contains two space-separated integer, and (, ) – the number of nodes the tree has and the number of variations.
Each of the next lines contains two space-separated integers and denoting that nodes and are connected by an edge ().
The next lines describe the variations. The first integer in the th line is (). Then space-separated integers follow: if the th number is , it means that Flóra adds a new leaf to node (). We may add more than one leaf to the same node. i.e. the sum of in all varations is at most .
After each variation, Flóra restarts and adds extra leaves to the original tree.
You should print lines. In the -th line, print a single integer: the minimum cost required to clean the -th variation of the tree. If the tree cannot be cleaned, print .
Scoring
Input
The first line contains two space-separated integer, and (, ) – the number of nodes the tree has and the number of variations.
Each of the next lines contains two space-separated integers and denoting that nodes and are connected by an edge ().
The next lines describe the variations. The first integer in the th line is (). Then space-separated integers follow: if the th number is , it means that Flóra adds a new leaf to node (). We may add more than one leaf to the same node. i.e. the sum of in all varations is at most .
After each variation, Flóra restarts and adds extra leaves to the original tree.
Output
You should print lines. In the -th line, print a single integer: the minimum cost required to clean the -th variation of the tree. If the tree cannot be cleaned, print .
Samples
Note
The following picture shows the second variation. A possible solution is to clean the path between leaves , and .
You can download the above example and an additional (bigger) sample input here: https://gofile.io/d/8QlbsS