#P8205. [Ynoi2005] vti
[Ynoi2005] vti
Problem Description
Given a tree with nodes, the edges are numbered from to . The -th edge has a weight . The root of the tree is node .
Define that two edges have an ancestor relationship if and only if, among the two endpoints of edge , the deeper node is an ancestor of the deeper node among the two endpoints of edge .
There are queries. In the -th query, (possibly repeated) nodes are given. Consider the union of edges on the simple paths between every pair of these nodes. Among these edges, count the number of ways to choose two unordered edges with different indices such that is an ancestor of , and .
Input Format
The first line contains an integer .
The next lines each contain two integers , meaning that there is an edge between and , with weight .
The next line contains an integer .
The next lines each contain integers. The first integer is , followed by integers representing the set of nodes in this query.
Output Format
For each query, output one line containing one integer, the answer.
5
1 1
2 2
3 4
1 3
3
1 1
2 1 3
3 3 4 5
0
1
3
Hint
Idea: nzhtl1477, Solution: nzhtl1477, Code: ccz181078, Data: nzhtl1477.
For of the testdata, , , , , , . All values above are integers.
Translated by ChatGPT 5