QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 512 MB 總分: 100

#17307. Higurashi When They Cry – Sotsu

统计

Given a tree with $n$ nodes, where each edge has a weight.

There are $m$ queries. For the $i$-th query, you are given $t_i$ nodes (not necessarily distinct). Consider the union of edges on the simple paths between every pair of these nodes. Find the number of ways to choose two distinct edges from this union such that the two edges have the same weight.

Input

The first line contains an integer $n$.

The next $n-1$ lines each contain two integers $a_i$ and $b_i$, representing an edge between $i+1$ and $a_i$ with weight $b_i$.

The next line contains an integer $m$.

The next $m$ lines each contain $t_i+1$ integers. The first integer is $t_i$, followed by $t_i$ integers representing the set of nodes for that query.

Output

For each query, output a single line containing an integer representing the answer.

Examples

Input 1

5
1 1
2 2
3 2
2 1
3
2 1 4
3 1 2 4
1 2

Output 1

1
1
0

Subtasks

Idea: nzhtl1477, Solution: s_r_f, Code: s_r_f, Data: nzhtl1477

For $100\%$ of the data, $1\le n\le 10^5$, $1\le m\le 10^5$, $1\le a_i\le i-1$, $1\le b_i\le n$, $1\le t_i$, and $t_1+\dots+t_m\le 10^5$.

All values above are integers.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.