#P11022. [2016杭电多校]Boss Bo
[2016杭电多校]Boss Bo
Boss Bo
Problem Description
There is a tree with nodes, whose root is 1. There are queries for you,for each query,we will give numbers,which are . For every node in the tree,we assume it's good if there is not a node ,such that is the ancient of or And we will give two numbers to show the property of each query. 1.when ,you should output the sum of the distance between every good node and . 2.when ,you should output the minimum distance between every good node and . 3.when ,you should output the maximum distance between every good node and . Let the distance between nodes in the tree be the shortest path between these two nodes.And we assume the length of each edge is 1. Specially,the distance between two same nodes is 0. For each query,if there is no nodes that is good,just output -1.
Input
There are several test cases. For each test case,the first line contains two numbers and . For the following lines,each line contains two numbers and ,indicating there is a edge between and in tree. For the following lines,each line contains some numbers,which are in order. Let the answer of last query be ,then . If the answer of last query is -1 or it's the first query,then . Let the number of test cases be ,we guarantee . test cases satisfy ,. test cases satisfy ~,,.
Output
You should output Q lines in total,each line contains a number indicating the answer of each query.
Sample Input
6 5
1 3
2 1
4 2
6 4
5 6
3 5 3
3 3 3
3 4 3
4 5 1
3 3 1
1 5 6
3 5 2
3 4 2
3 3 3
6 5 3
Sample Output
3
-1
-1
3
2
Author
绍兴一中
Source
2016 Multi-University Training Contest 3