elca

ELCA

You have a rooted tree containing n vertexes. Let's index these tree vertexes with integers from 1 to n. The tree root is vertex 1.

Each vertex (except for the tree root) v has a direct ancestor pv. Also, each vertex v has an integer value sv.

Your are to perform the following queries:

  • P v u (uv). If u isn't in the subtree of v, you must perform the assignment pv=u. Otherwise you must perform the assignment pu=v. Note that after this query the graph continues to be a tree consisting of n vertexes.
  • V v t. Perform the assignment sv=t.
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 i and j. Here, the lowest common ancestor of i and j is the deepest vertex that lies on both the path from the root to vertex i and the path from the root to vertex j. Please note that the vertices i and j can be the same (in this case their lowest common ancestor coincides with them). When i and j are different, the pair (i,j) is considered distinct from (j,i).

Input format

The first line of the input contains an integer n (2n50000) — the number of tree vertexes.

The second line contains n1 integers p2,p3,...,pn (1pin) — the direct ancestor of every vertex. It is guaranteed that these edges form a tree.

The third line contains n integers — s1,s2,...sn (0si106) — the values written on each vertex of the tree.

The next line contains an integer q (1q50000) — the number of queries. Each of the following q lines contains the description of a query in the format described in the statement. It is guaranteed that query arguments u and v lie between 1 and n. It is guaranteed that argument t in the queries of type V meets limits 0t106.

Output format

Print q+1 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 106.

Limits

Subtask # Score Constraints
1 11 n,q200
2 23 n,q2000
3 22 All queries will be of type V.
4 24 n,q25000
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 v u, if u lies in subtree of v you must perform the assignment pu=v. An example of such a case is the last query in the sample.




Time Limit: 4 Seconds
Memory Limit: 1024MB
Your best score: 0
Source: Codeforces 482E

Subtask Score
1 11
2 23
3 22
4 24
5 20
6 0