QOJ.ac

QOJ

時間限制: 5 s 記憶體限制: 1024 MB 總分: 100

#18824. Tree and Query 21

统计

There is a tree consisting of $N$ vertices. The vertices are numbered from $0$ to $N-1$. The edges have non‑negative integer weights.

You need to process the following queries.

  1. 1 u v w x: Remove the edge connecting $u$ and $v$, and add an edge of weight $x$ connecting $v$ and $w$. It is guaranteed that there exists an edge between $u$ and $v$. After the operation, the graph remains a tree. ($0 \le x \le 100\,000$)
  2. 2 k x_1 x_2 ... x_k: For the vertices $x_1, x_2, \ldots, x_k$, consider all unique simple paths between every pair $x_i$ and $x_j$ (for all $1 \le i < j \le k$). Output the sum of weights of all edges that belong to the union of these paths. All $x_i$ are distinct. ($1 \le k \le n$)

Input

The first line contains $N$, the size of the tree. ($2 \le N \le 100{,}000$)

The next $N-1$ lines each contain three integers $u, v, w$, denoting an edge of weight $w$ connecting vertices $u$ and $v$. ($0 \le u, v \le N - 1$, $0 \le w \le 100{,}000$)

The next line contains $Q$, the number of queries. ($1 \le Q \le 100{,}000$)

The following $Q$ lines contain queries as described above.

The sum of $k$ over all type 2 queries is between $1$ and $100{,}000$ inclusive.

Output

For each type 2 query, output the result on a separate line, in order.

Examples

Input 1

7
0 1 1
0 2 2
1 3 3
1 4 4
2 5 5
2 6 6
4
1 1 4 5 7
2 3 0 4 6
1 0 2 1 8
2 3 0 4 6

Output 1

20
27

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.