CF482E.ELCA
NOI/NOI+/CTSC
通过率:0%
时间限制:8.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You have a root tree containing n vertexes. Let's number the tree vertexes with integers from 1 to n. The tree root is in the vertex 1.
Each vertex (except fot the tree root) v has a direct ancestor p__v. Also each vertex v has its integer value s__v.
Your task is to perform following queries:
- P v u (u ≠ v). If u isn't in subtree of v, you must perform the assignment p__v = u. Otherwise you must perform assignment p__u = v. Note that after this query the graph continues to be a tree consisting of n vertexes.
- V v t. Perform assignment s__v = t.
Your task is following. Before starting performing queries and after each query you have to calculate expected value written on the lowest common ancestor of two equiprobably selected vertices i and j. Here lowest common ancestor of i and j is the deepest vertex that lies on the both of 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).
你有一棵包含 n 个顶点的有根树。我们将树的顶点编号为 1 到 n,其中根节点位于顶点 1。
每个顶点(除根节点外)v 都有一个直接父节点 pv。此外,每个顶点 v 还有一个整数值 sv。
你需要执行以下两类查询:
P v u(其中 u=v)。若 u 不在 v 的子树中,则执行赋值操作 pv=u;否则执行赋值操作 pu=v。注意:执行该查询后,图仍是一棵包含 n 个顶点的树。V v t。执行赋值操作 sv=t。
你的任务如下:在开始执行查询之前,以及每次查询执行完毕之后,你都需要计算:随机等概率地选取两个顶点 i 和 j(允许 i=j),它们的最近公共祖先(LCA)上所写数值的期望值。此处,i 与 j 的最近公共祖先定义为同时位于从根到顶点 i 的路径、以及从根到顶点 j 的路径上的深度最大的顶点。请注意,当 i=j 时,其最近公共祖先即为 i(或 j)自身。
输入格式
The first line of the input contains integer n (2 ≤ n ≤ 5·104) — the number of the tree vertexes.
The second line contains n - 1 integer _p_2, _p_3, ..., p__n (1 ≤ p__i ≤ n) — the description of the tree edges. It is guaranteed that those numbers form a tree.
The third line contains n integers — _s_1, _s_2, ... s__n (0 ≤ s__i ≤ 106) — the values written on each vertex of the tree.
The next line contains integer q (1 ≤ q ≤ 5·104) — the number of queries. Each of the following q lines contains the description of the 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 0 ≤ t ≤ 106.
输入的第一行包含一个整数 $ n ( 2 \leq n \leq 5 \cdot 10^4 $)——树的顶点数量。
第二行包含 $ n-1 $ 个整数 $ p_2, p_3, \dots, p_n ( 1 \leq p_i \leq n $)——树的边的描述。保证这些数字构成一棵树。
第三行包含 $ n $ 个整数——$ s_1, s_2, \dots, s_n ( 0 \leq s_i \leq 10^6 $)——树中每个顶点上所写的数值。
接下来一行包含一个整数 $ q ( 1 \leq q \leq 5 \cdot 10^4 $)——查询的数量。随后的 $ q $ 行每行包含一个按题面所述格式描述的查询。保证查询中的参数 $ u $ 和 $ v $ 均在 $ 1 $ 到 $ n $ 之间。保证类型为 V 的查询中的参数 $ t $ 满足限制 $ 0 \leq t \leq 10^6 $。
输出格式
Print q + 1 number — the corresponding expected values. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 9.
输出 q+1 个数——对应的期望值。若你的答案的绝对或相对误差不超过 10−9,则视为正确。
输入输出样例
输入#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
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 assignment p__u = v. An example of such case is the last query in the sample.
注意,在查询 Pvu 中,如果 u 位于 v 的子树中,则必须执行赋值操作 pu=v。样例中的最后一个查询即为这种情况的一个示例。
输入解题思路,AI测评打分。不知道怎么写?