Namunamu, Time to Plant Trees
Time limit2sMemory limit512 MB
Given a rooted tree with fruit weights, process queries that graft a new weighted vertex onto a vertex and report the fruit weight sum on the path from a vertex to the root.
- Level
Medium5 of 10
- Topics
- Tree, DFS, Implementation
- Solved
- No attempts yet
Problem
Geunseong is fond of trees.
He is a developer now, but his love for trees has not faded. One day, while playing with binary trees, Geunseong realized that a binary tree is a tree yet bears no fruit, and he was deeply shocked. Insisting that trees must bear fruit, he built a tree graph that bears fruit and named it "Namunamu".
The features of Namunamu are as follows.
- Vertex of the tree is the root. The tree grows upward from the root.
- Vertices other than are the points where a branch forks or ends. A branch can be connected to a vertex. However, branches forked from different vertices do not merge at a vertex.
- An edge is a branch. Both ends of a branch must be vertices.
- A vertex can bear at most one fruit. The root bears no fruit. Before each query, every vertex that can bear fruit does bear fruit.
After building the tree, Geunseong came up with two kinds of queries.
1 i j w: (Grafting) Attach a branch to an arbitrary vertex .- Vertex belongs to the same graph as the root.
- Since a branch always ends at a vertex, the vertex numbered is created at the far end of the new branch.
- Vertex bears a fruit of weight . means no fruit grows there.
- ( )
2 i: (Harvest) Output the force needed to shake vertex so that every fruit on vertex and on all vertices connected above it falls.- Shaking vertex shakes every branch and vertex connected above it with the same force.
- To knock down all the fruit, the force must equal the sum of the fruit weights on vertex and on all vertices connected above it.
- Shaking with force is not allowed, so if the total weight is , output
-1. - ()
The input values are all integers, and the input is guaranteed to be valid. There is at least one harvest query.
Geunseong left for the clubroom in the middle of writing these queries. Please solve them for him.
Input
The first line contains the initial number of vertices and the number of queries , separated by a space. The vertices initially have distinct numbers from to . ()
The second line contains, for each vertex from to , the vertex directly above it that it is connected to by a branch, separated by spaces. Vertex is the root, so it is given as -1. Every vertex eventually belongs to the same graph as the root, but some may not yet belong to it during the input.
The third line contains the fruit weight of each vertex from to , separated by spaces. () A value of means no fruit grows there. The root bears no fruit.
The next lines each contain one query.
Output
For each harvest query, output the force needed to shake the vertex. If that force is , output -1.
Hint
The first sample starts with the initial tree shape. In the first query, shaking vertex also shakes vertices and . Vertex holds a fruit that needs a force of to knock down, so the force is .

This picture shows the tree after three grafts. Shaking vertex shakes only vertex , and knocking down its fruit requires a force of . Likewise, shaking vertex shakes vertices , , and . Knocking down the fruit on vertices and requires a force of .
In the second sample, the first query shakes vertex , which also shakes vertices , , and . None of them bears a fruit, so the force is and the output is -1.

Shaking vertex shakes vertices , , , and . Knocking down the fruit on vertex requires a force of .