Tree and Queries 20
Time limit5sMemory limit512 MB
Maintain a dynamic forest with link/cut and weighted edges, supporting toggling a vertex weight and querying the minimum weighted sum of tree distances from any vertex, with encrypted vertex indices.
- Level
Hard10 of 10
- Topics
- Tree, Segment tree, Union-find
- Solved
- No attempts yet
Problem
There is a forest with vertices. The vertices are numbered from to , and initially there are no edges. Each vertex has an integer , and every initial value is .
Write a program that processes the following queries.
1 a b c: connect vertices and with an edge of weight . This query is given only when the graph remains a forest after it is performed.2 a b: remove the edge connecting vertices and . This query is given only when that edge exists.3 a: first replace with . Then, for the tree containing vertex , let its vertices be and output the minimum value below.
Here, is the sum of the weights of all edges on the path from to .
Input
The first line contains the number of vertices and the number of queries . Each of the next lines contains one query.
The vertex numbers appearing in queries are encrypted and must be decoded before the query is executed. If the input vertex number is and the sum of all outputs from type queries processed so far is , then the actual vertex number is
Before the first type query, use . The edge weight in a type query is not encrypted.
Output
For each type query, output the computed value on its own line, in the order the queries are given.