Library - MarisaOJ: Marisa Online Judge

Library

Time limit: 2000 ms
Memory limit: 512 MB
The Scarlet Devil Mansion's great library can be represented by a tree with $n$ vertices. The entrance of the library is at vertex $1$, and the bookshelves are at the remaining vertices. Some bookshelves contain valuable books, so Patchouli wants to block some paths between bookshelves so that Marisa cannot reach any bookshelf containing valuable books. The strength to block a path may vary between different pairs of vertices. There are many ways to block paths that satisfy the condition, and Patchouli wants to choose the method that requires the least strength because she has asthma. There are $q$ days, and on the $i$-th day, there are $k_i$ bookshelves containing valuable books. Help Patchouli calculate the minimum total strength required to block the paths. ### Input - The first line contains an integer $n$. - The next $n - 1$ lines each contain three integers $u, v, w$, indicating an edge between $u$ and $v$, and $w$ strength is needed to block the path on that day. - The next line contains an integer $q$. - The next $q$ lines, where the $i$-th line starts with an integer $k_i$, followed by $k_i$ integers representing the indices of bookshelves containing valuable books. ### Output - For each query, print an integer representing the minimum total strength required to block the paths. ### Constraints - $1 \le n \le 2.5 \times 10^5$. - $1 \le q \le 5 \times 10^5$. - $1 \le u, v \le n$. - $1 \le \sum{k_i} \le 5 \times 10^5$, $1 \le k_i \le n - 1$. - $1 \le w \le 10^5$. ### Example Input: ``` 10 1 5 13 1 9 6 2 1 19 2 4 8 2 3 91 5 6 8 7 5 4 7 8 31 10 7 9 3 2 10 6 4 5 7 8 3 3 9 4 6 ``` Output: ``` 12 32 22 ```