Universal Cup Judging System

Universal Cup

Límite de tiempo: 4.0 s Límite de memoria: 256 MB Puntuación total: 100 Hackeable ✓
Estadísticas

$N$ 個の頂点と $N - 1$ 本の辺を持つ木が与えられます。各辺 $i$ には複雑度 $l_i$ があります。2 つの頂点 $u$ と $v$ の間の距離 $d(u, v)$ は、木におけるそれらを結ぶ唯一のパス上の複雑度の総和です。なお、$d(u, v) = d(v, u)$ かつ $d(v, v) = 0$ です。

$M$ 個のパズルがあり、各パズル $i$ には $k_i$ 個の情報ピースが必要です。これらの情報ピースは木の特定の辺の上に配置されています。同じ辺の上に複数の情報ピースが存在することもあります!

任意の 2 つの頂点 $S$ と $T$ について、$S$ から $T$ へのパス上で遭遇したすべてのパズルに必要な情報ピースがすべて集められた場合、$S$ と $T$ の間の旅は成功となります。具体的には、あるパズルに必要なすべての情報ピースにパス上で遭遇した場合、そのパズルはその旅の間で解くことができるとみなされます。

課題は、任意の 2 つの頂点 $S$ と $T$(ただし $1 \le S \le T \le N$)の間のすべての成功した旅についての距離の総和を、$10^9 + 7$ で割った余りを求めることです。

形式的には、次を計算することが課題です:

$$\sum_{\substack{1 \le S \le T \le N, \\ \text{journey } (S, T) \text{ is successful}}} d(S, T)$$

頂点 $S$ から出発し、木の上の唯一のパスに沿って別の頂点 $T$ まで移動する必要があります。途中、このパスの辺上にある情報ピースは自動的に収集されます。$S$ から $T$ への旅が成功とみなされるためには、以下の条件を満たす必要があります:

  • 旅の途中で少なくとも 1 つの情報ピースが収集されたパズルの集合を $O$ とします。集合 $O$ に含まれるすべてのパズルを解くために必要な情報ピースがすべて収集された場合、かつその場合に限り、その旅は成功となります。言い換えれば、$O$ 内の各パズルを完全に解くために必要なすべての情報ピースを集めていなければなりません。

入力

1 行目には空白で区切られた 2 つの整数 $N$ と $M$ ($2 \le N \le 10^6, 1 \le M \le 10^6$) が与えられます。

続く $N - 1$ 行には、それぞれ空白で区切られた 3 つの整数 $a_i, b_i, l_i$ ($1 \le a_i, b_i \le N, 1 \le l_i \le 10^9$) が与えられ、頂点 $a_i$ と頂点 $b_i$ を結ぶ複雑度 $l_i$ の辺 $i$ を表します。

続いて $M$ 個のパズルの定義が与えられます。各パズル $i$ について、入力は $k_i$ と、それに続く空白で区切られた $k_i$ 個の整数 $u_{i_1}, u_{i_2}, \dots, u_{i_{k_i}}$ ($0 \le k_i \le K, 1 \le u_{i_j} \le N - 1$) を含む 1 行で構成されます。ここで $u_{i_j}$ はそのパズルを解くために必要な情報ピースが配置されている辺の番号です。

ここで、$K$ はすべてのパズルにおける $k_i$ の値の総和であり、$1 \le K \le 10^6$ であることが保証されます。

出力

問題の答えを $10^9 + 7$ で割った余りを表す 1 つの整数を 1 行に出力してください。

入出力例

入力例 1

5 4
1 2 3
1 4 1
1 5 6
4 3 2
2 4 4
1 1
0
2 3 4

出力例 1

17

ノート

problem_15524_1.png

図 1. 木の構造。ひし形は情報ピースを表し、辺の上の数値は複雑度を表します。

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.