#P17268. [ICPC 2017 Urumqi R] Lowest Common
[ICPC 2017 Urumqi R] Lowest Common
Problem Description
In graph theory the lowest common ancestor of two nodes and in a rooted tree is the deepest node that has both and as descendants. Here we define each node to be a descendant of itself. The LCA of two nodes in is the shared ancestor of them that is located farthest from the root.
But how about an un-rooted tree?
In this problem you are given an un-rooted tree with nodes labelled from to and several pairs of nodes .
For each node of , consider the tree rooted by which becomes a rooted tree; and calculate the summation .
Input Format
The input has several test cases and the first line contains an integer which is the number of test cases.
For each test case, the first line contains two integers and . Each of the following lines describes an edge with two integers and . Then following lines contain pairs of nodes described as above .
Both of the sum of and the sum of in input are smaller than .
Output Format
For each test case, output a line with integers.
The -th one is the value of corresponding to the tree rooted by the -th node.
2
4 3
1 2
1 3
1 4
2 3
3 4
2 4
4 1
1 2
2 3
3 4
1 4
3 5 7 9
1 2 3 4