elca
ELCA
You have a rooted tree containing vertexes. Let's index these tree vertexes with integers from to . The tree root is vertex .
Each vertex (except for the tree root) has a direct ancestor . Also, each vertex has an integer value .
Your are to perform the following queries:
- P . If isn't in the subtree of , you must perform the assignment . Otherwise you must perform the assignment . Note that after this query the graph continues to be a tree consisting of vertexes.
- V . Perform the assignment .
The effects of each query carry over to subsequent queries.
Your task is as follows. Before the first query, and after each query, you have to calculate the expected value written on the lowest common ancestor of two equiprobably selected vertices and . Here, the lowest common ancestor of and is the deepest vertex that lies on both the path from the root to vertex and the path from the root to vertex . Please note that the vertices and can be the same (in this case their lowest common ancestor coincides with them). When and are different, the pair is considered distinct from .
Input format
The first line of the input contains an integer — the number of tree vertexes.
The second line contains integers — the direct ancestor of every vertex. It is guaranteed that these edges form a tree.
The third line contains integers — — the values written on each vertex of the tree.
The next line contains an integer — the number of queries. Each of the following lines contains the description of a query in the format described in the statement. It is guaranteed that query arguments and lie between and . It is guaranteed that argument in the queries of type meets limits .
Output format
Print lines each containing a real number — the corresponding expected values. Your answer will be considered correct if its absolute or relative error doesn't exceed .
Limits
Subtask #
|
Score
|
Constraints
|
1 |
11 |
|
2 |
23 |
|
3 |
22 |
All queries will be of type V. |
4 |
24 |
|
5 |
20 |
No additional constraints |
Samples
Sample Input 1
|
Sample Output 1
|
5
1 2 2 1
1 2 3 4 5
5
P 3 4
P 4 5
V 2 3
P 5 2
P 1 4
|
1.640000000
1.800000000
2.280000000
2.320000000
2.800000000
1.840000000
|
Note that in the query P , if lies in subtree of you must perform the assignment . An example of such a case is the last query in the sample.