P13828 [Ynoi Easy Round 2026] 寒蝉鸣泣之时·卒

题目背景

![](https://cdn.luogu.com.cn/upload/image_hosting/hseawe3v.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/dt95xwcc.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/5tqz3mf5.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/qtrk5cu5.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/ule5ei9v.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/sdlkqfbx.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/02kcxdoj.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/vuu7hki7.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/smco7vmo.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/5mgo4bqw.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/5rnqmygo.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/ixjvbqin.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/5j76v0is.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/5hlyfa70.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/3yt5n0oy.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/b9557ak1.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/8k61bijo.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/s0tecnlk.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/epmaccna.png)

题目描述

给定一棵包含 $n$ 个结点的树,每条边有一个权值; 共 $m$ 次询问,第 $i$ 次询问给出 $t_i$ 个不保证互不相同的点,考虑这些点两两之间简单路径经过的边的并集,问无序地选出两条不同的边,使得边权相同的方案数。

输入格式

第一行一个整数 $n$。 接下来 $n-1$ 行,每行两个整数 $a_i,b_i$,表示 $i+1$ 和 $a_i$ 之间有一条边,权值为 $b_i$。 接下来一行一个整数 $m$。 接下来 $m$ 行,每行 $t_i+1$ 个整数,第一个整数为 $t_i$,之后 $t_i$ 个整数表示这次询问的点的集合。

输出格式

对每个询问,输出一行,包含一个整数,表示答案。

说明/提示

Idea:nzhtl1477,Solution:s_r_f,Code:s_r_f,Data:nzhtl1477 对于 $100\%$ 的数据,满足 $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$,$t_1+\dots+t_m\le 10^5$. 以上所有数值为整数。