#P2495. 【模板】虚树 / [SDOI2011] 消耗战

    ID: 1509 远端评测题 2000ms 505MiB 尝试: 1 已通过: 0 难度: 8 上传者: 标签>2011各省省选山东最近公共祖先,LCA虚树

【模板】虚树 / [SDOI2011] 消耗战

Description

In a war, the battlefield consists of nn islands connected by n1n-1 bridges, and there is exactly one simple path between any two islands. Our intelligence has located the enemy headquarters on island 11. The enemy lacks sufficient energy to sustain combat, and victory is in sight. It is known that there are rich energy reserves on another kk islands. To prevent the enemy from obtaining energy, our task is to blow up some bridges so that the enemy cannot reach any energy-rich island from island 11. Since different bridges have different materials and structures, the cost to destroy each bridge varies. We want to minimize the total cost while achieving the objective.

Intelligence also found that the enemy has a mysterious machine. Even after we cut off all energy, they can use that machine. The machine will not only repair all bridges we blew up, but also randomly redistribute the energy (with the guarantee that no energy will be placed on island 11). However, the machine can be used only mm times, so we only need to complete the task for each use.

Input Format

The first line contains an integer nn, the number of islands.

The next n1n-1 lines each contain three integers u,v,wu, v, w, indicating that islands uu and vv are directly connected by a bridge with destruction cost ww.

The (n+1)(n+1)-th line contains an integer mm, the number of times the enemy can use the machine.

Then follow mm lines. On the ii-th line, there is an integer kik_i, meaning that after the ii-th reset there are kik_i energy-rich islands, followed by kik_i integers h1,h2,,hkih_1, h_2, \ldots, h_{k_i}, which are the indices of the energy-rich islands.

Output Format

Output mm lines. For each use of the machine, output the minimum total cost required to prevent the enemy from reaching any energy-rich island from island 11.

10
1 5 13
1 9 6
2 1 19
2 4 8
2 3 91
5 6 8
7 5 4
7 8 31
10 7 9
3
2 10 6
4 5 7 8 3
3 9 4 6

12
32
22

Hint

Constraints

  • For 10%10\% of the testdata, n10n \leq 10, m5m \leq 5.
  • For 20%20\% of the testdata, n100n \leq 100, m100m \leq 100, 1ki101 \leq k_i \leq 10.
  • For 40%40\% of the testdata, n1000n \leq 1000, 1ki151 \leq k_i \leq 15.
  • For 100%100\% of the testdata, 2n2.5×1052 \leq n \leq 2.5\times 10^5, 1m5×1051 \leq m \leq 5\times 10^5, ki5×105\sum k_i \leq 5\times 10^5, 1ki<n1 \leq k_i < n, hi1h_i \neq 1, 1u,vn1 \leq u, v \leq n, 1w1051 \leq w \leq 10^5.

Translated by ChatGPT 5