Butea

Time limit: 4s Memory limit: 512MB Input: Output:

Little IR12660 decided to make a detour in Butea.\text{Little IR12660 decided to make a detour in Butea}.

Task

You are given a weighted binary tree with nn nodes and qq queries.

For the ii-th query (1iq1 \leq i \leq q):

  • you are given an integer kik_i and a set of kik_i nodes S1,S2,,SkiS_1, S_2, \dots, S_{k_i};
  • you must choose another set of exactly kik_i nodes T1,T2,,TkiT_1, T_2, \dots, T_{k_i};

Note that TT can also contain nodes from SS (or even T=ST=S)

  • your goal is to minimize the following value: a=1kib=1kidist(Sa,Tb)\sum_{a=1}^{k_i} \sum_{b=1}^{k_i} \mathrm{dist}(S_a, T_b) where dist(u,v)\mathrm{dist}(u,v) denotes the length of the shortest path between nodes uu and vv in the tree.

Input data

The first line contains two integers nn and qq --- the number of nodes and the number of queries.

The next nn - 11 lines describe the edges of the tree. On the ii-th of these lines, three integers uiu_i, viv_i, and wiw_i are given, indicating that there is an edge between nodes uiu_i and viv_i with weight wiw_i.

The next qq lines describe the queries. On the ii-th of these lines are given:

  • an integer kik_i,
  • followed by kik_i distinct integers representing the nodes S1,S2,,SkiS_1, S_2, \dots, S_{k_i}.

The values written on the same line are separated by a single space.

Output data

The output will contain qq lines. On line jj (1jq1 \leq j \leq q) output a single integer --- the minimum possible value of the expression corresponding to the jj-th query.

Constraints and clarifications

  • 1n,q21051 \leq n, q \leq 2 \cdot 10^5
  • 1wi101 \leq w_i \leq 10 for all 1in1 \leq i \leq n.
  • i=1qki2105\sum_{i=1}^{q} k_i \leq 2 \cdot 10^5.
  • 1Sin1 \le S_i \le n for all queries.
  • It is guaranteed that the given edges form a binary tree (i.e. for a suitable choice of the root node each node has at most 2 children).
# Score Constraints
0 0 Examples
1 10 n,q200,i=1qki400n, q \leq 200, \sum_{i=1}^{q} k_i \le 400
2 15 n,q2000,i=1qki4000n, q \leq 2000, \sum_{i=1}^{q} k_i \le 4000
3 20 The tree is a chain (i.e. each node has degree at most 22)
4 55 No further restrictions.

Example

stdin

10 5
3 4 8
10 9 2
8 1 1
4 10 4
7 1 2
10 5 5
6 8 9
1 3 8
3 2 5
3 8 2 7
5 6 7 8 10 9
4 2 10 9 3
5 6 8 1 9 4
3 2 1 5

stdout

51
306
128
280
103

Explanation

The tree from the sample input. Bold nodes correspond to the first query..\text{The tree from the sample input. Bold nodes correspond to the first query.}.

For the first query, picking T={1,8,7}T=\{1,8,7\} yields a cost of 51:

  • From node 11, the cost is dist(1,7)+dist(1,8)+dist(1,2)=2+1+13=16.\operatorname{dist}(1,7)+\operatorname{dist}(1,8)+\operatorname{dist}(1,2) = 2+1+13 = 16.
  • From node 88, the cost is dist(8,7)+dist(8,8)+dist(8,2)=3+0+14=17.\operatorname{dist}(8,7)+\operatorname{dist}(8,8)+\operatorname{dist}(8,2)= 3+0+14 = 17.
  • From node 77, the cost is dist(7,7)+dist(7,8)+dist(7,2)=0+3+15=18.\operatorname{dist}(7,7)+\operatorname{dist}(7,8)+\operatorname{dist}(7,2)= 0+3+15 = 18.

16+17+18=5116+17+18 = 51.

Log in or sign up to be able to send submissions!