Koothrappali and Expected Value

0

0 votes
Medium
Problem

Koothrappali is hungry and it's almost dinner time. So, he must find a good place to have dinner. He has this weird habit of having dinner at two different places and then comparing which one was better. He has located N restaurants near him from which he'll choose two places to have dinner. These N restaurants are connected by N - 1 roads and one can reach one restaurant from another using a combination of these roads.

Since he is an introvert, he doesn't want to travel much between restaurants. He has chosen X restaurants out of these N restaurants. Now, he'll choose two restaurants randomly from these X restaurants. He has asked you to find the expected value of distance between the chosen two restaurants. Can you help him?

Input Format:

First line consists of N, number of restaurants.

Each of the next N - 1 lines consists of 3 integers U, V and W, denoting that there is a road of length W between restaurants U and V.

Next line contains a single integer Q, number of queries.

Each of the next Q lines contain an integer X followed by X integers denoting the restaurants chosen by Koothrappali.

Output Format:

For each query, output the required expected value in form of an irreducible fraction P/Q, that is, gcd(P, Q) must be equal to 1.

Constraints:

2N105

1U,VN

1W105

1Q10

2XN

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For first query, there is only one pair possible, that is, restaurant 1 to restaurant 4 with length of 6 units.

For second query, there are 3 pairs possible with path length equal to 2, 3, and 5. Hence, expected value is 10/3.

For third query, there are 10 pairs possible with path lengths equal to 2, 3, 6, 7, 5, 4, 5, 9, 10, 9. Hence, expected value is 6/1.

Editor Image

?