Universal Cup Judging System

Universal Cup

حد الوقت: 4.0 s حد الذاكرة: 256 MB مجموع النقاط: 100 قابلة للهجوم ✓
الإحصائيات

給定一棵擁有 $N$ 個節點和 $N - 1$ 條邊的樹,其中每條邊 $i$ 都有一個複雜度 $l_i$。兩個節點 $u$ 和 $v$ 之間的距離 $d(u, v)$ 定義為樹上連接它們的唯一路徑上所有邊的複雜度之和。需要注意的是,$d(u, v) = d(v, u)$ 且 $d(v, v) = 0$。

有 $M$ 個謎題,每個謎題 $i$ 需要 $k_i$ 個資訊碎片,這些資訊碎片位於樹上的特定邊上。同一條邊上可能會有多個資訊碎片!

對於任意兩個節點 $S$ 和 $T$,如果從 $S$ 到 $T$ 的路徑上,所有遇到的謎題所需的資訊碎片都被收集完整,則稱 $S$ 和 $T$ 之間的旅程是成功的。具體來說,如果某個謎題所需的所有資訊碎片都在路徑上被遇到,則該謎題在旅程中被認為是可解的。

任務是計算任意兩個節點 $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$ 的旅程被認為是成功的,它必須滿足以下條件:

  • 設 $O$ 為在旅程中收集到至少一個資訊碎片的謎題集合。當且僅當解決集合 $O$ 中每個謎題所需的所有資訊碎片都已被收集時,該旅程才是成功的。換句話說,你必須收集到完全解決 $O$ 中每個謎題所需的每一件資訊。

輸入格式

第一行包含兩個空格分隔的整數 $N$ 和 $M$($2 \le N \le 10^6, 1 \le M \le 10^6$)。

接下來 $N - 1$ 行,每行包含三個空格分隔的整數 $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$),其中 $u_{i_j}$ 是解決該謎題所需資訊碎片所在的邊的編號。

這裡 $K$ 是所有謎題的 $k_i$ 值之和,保證 $1 \le K \le 10^6$。

輸出格式

輸出一行,包含一個整數,即問題的答案對 $10^9 + 7$ 取模後的結果。

範例

輸入範例 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.