HH Country
Time limit10sMemory limit512 MB
For each query set of tree vertices, output twice the sum of pairwise tree distances.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Prefix sum
- Solved
- No attempts yet
Problem
HH is the strongest country in competitive programming. It has cities numbered 1 to , and the cities are connected by roads. Between any two different cities there is exactly one path, so the cities and roads of HH form a tree.
HH runs a contest to split the budget of the Forward-looking Infrastructure Development Program. The contest has rounds, and round decides how budget is distributed. The distribution follows the result of a double round robin among the cities attached to that budget. For two different participating cities A and B, one game is played with A travelling to B, and one game is played with B travelling to A. A round therefore holds games in total.
To itemize the travel expenses, HH needs the total travelling distance between the participating cities of each round. The distance of one game is the number of roads on the only path from the away city to the home city. For every round, compute the sum of that distance over all games of the round.
Input
The first line contains an integer , the number of test cases.
The first line of each test case contains two integers and , the number of cities and the number of rounds. Each of the next lines contains two integers and , meaning there is a road between city and city . Each of the next lines begins with an integer , the number of cities taking part in round , followed on the same line by the labels of those cities.
- All in one round are distinct.
- within one test case.
- The input file is not larger than 60MB.
Output
For each round, print the total travelling distance of that round on its own line, in the order the rounds are given.