#P8205. [Ynoi2005] vti

[Ynoi2005] vti

Problem Description

Given a tree with nn nodes, the edges are numbered from 11 to n1n-1. The ii-th edge has a weight bib_i. The root of the tree is node 11.

Define that two edges x,yx, y have an ancestor relationship if and only if, among the two endpoints of edge xx, the deeper node is an ancestor of the deeper node among the two endpoints of edge yy.

There are mm queries. In the ii-th query, tit_i (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 x,yx, y with different indices such that xx is an ancestor of yy, and bx<byb_x < b_y.

Input Format

The first line contains an integer nn.

The next n1n-1 lines each contain two integers ai,bia_i, b_i, meaning that there is an edge between i+1i+1 and aia_i, with weight bib_i.

The next line contains an integer mm.

The next mm lines each contain ti+1t_i+1 integers. The first integer is tit_i, followed by tit_i 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 100%100\% of the testdata, 1n1051 \le n \le 10^5, 1m1061 \le m \le 10^6, 1aii11 \le a_i \le i-1, 1bin1 \le b_i \le n, 1ti1 \le t_i, t1++tm106t_1 + \dots + t_m \le 10^6. All values above are integers.

Translated by ChatGPT 5