Bear and Destroying Subtrees

时间:2020-04-10
本文章向大家介绍Bear and Destroying Subtrees,主要包括Bear and Destroying Subtrees使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Bear and Destroying Subtrees

Limak is a little grizzly bear. He will once attack Deerland but now he can only destroy trees in role-playing games. Limak starts with a tree with one vertex. The only vertex has index 1 and is a root of the tree.

Sometimes, a game chooses a subtree and allows Limak to attack it. When a subtree is attacked then each of its edges is destroyed with probability , independently of other edges. Then, Limak gets the penalty — an integer equal to the height of the subtree after the attack. The height is defined as the maximum number of edges on the path between the root of the subtree and any vertex in the subtree.
You must handle queries of two types.

1 v denotes a query of the first type. A new vertex appears and its parent is v. A new vertex has the next available index (so, new vertices will be numbered 2, 3, ...).

2 v denotes a query of the second type. For a moment let's assume that the game allows Limak to attack a subtree rooted in v. Then, what would be the expected value of the penalty Limak gets after the attack?

In a query of the second type, Limak doesn't actually attack the subtree and thus the query doesn't affect next queries.

Input

The first line of the input contains one integer q (1 ≤ q ≤ 500 000) — the number of queries.

Then, q lines follow. The i-th of them contains two integers typei and vi (1 ≤ typei ≤ 2). If typei = 1 then vi denotes a parent of a new vertex, while if typei = 2 then you should print the answer for a subtree rooted in vi.

It's guaranteed that there will be at least 1 query of the second type, that is, the output won't be empty.

It's guaranteed that just before the i-th query a vertex vi already exists.

Output

For each query of the second type print one real number —the expected value of the penalty if Limak attacks the given subtree. Your answer will be considered correct if its absolute or relative error does not exceed \(10^{-6}\).

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if \(\frac{|a-b|}{max(1,b)}\leq 10^{-6}\).

Sample Input

7

1 1

1 1

2 1

1 2

1 3

2 2

2 1

Sample Output

0.7500000000

0.5000000000

1.1875000000

思路:

先考虑普通\(dp\),令\(dp_{ij}\)表以\(i\)为根的子树高\(j\)的概率,可得\(:dp_{ij}=\)!^%!#&@%#!(

我们发现这样很难推式子,所以我们不妨令\(dp_{ij}\)表示树高小于等于\(j\)的概率,
可得\(:dp_{ij}=\prod\limits^u_{u\in son_i}\frac{dp_{uj-1}+1}{2}\),这就很和谐了,查询的时候用容斥查询,但空间复杂度\(O(q^2)\),每次加点时又需从下往上重新修改,时间复杂度\(O(q^3)\)

回归式子,我们可以发现单个结点的\(dp\)值在往上更新时会不断地除\(2\),所以一个节点的\(dp_{ij}\)对其某祖先节点的\(dp_{fd}\)(乘积的)贡献\(x\)与距离\(d-j\)\(x=1-\frac{1-dp_{ij}}{2^x}(每次上传时都与1取平均值)\),所以当\(d-j\)足够大时就可以忽略不计了,直接当作\(1\)了。由此式子我们也可以发现树高很高的概率奇低。

所以设参数\(X\),表示我们\(dp\)中保存的最大树高以及加点上传时的最长距离。
时间复杂度\(O(qX^2)\),一般取\(40 \leq X \leq 50\)

\(\mathfrak{Talk\ is\ cheap,show\ you\ the\ code.}\)

#include<map>
#include<cstdio>
#include<queue>
#include<bitset>
#include<vector>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
# define Type template<typename T>
# define ll long long
# define ull unsigned ll
# define read read1<int>()
Type inline T read1(){
    T t=0;
    char k;
    bool fl=0;
    do k=getchar(),(k=='-')&&(fl=1);while('0'>k||k>'9');
    while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
    return fl?-t:t;
}
# define N 50
# define M 500005
double dp[M][N];
int f[M],s,t=1,q[M];
void ind(double *n){
    for(int i=0;i<N;++i)
        n[i]=1;
}
void work(int n,int x){
    ind(dp[n]);
    q[0]=0;
    for(int i=0,j=x;i<N&&j;++i,j=f[j])q[++q[0]]=j;
    for(int i=q[0],j,k;i>1&&(j=q[i-1],k=q[i]);--i)
        for(int h=1;h<N;++h)    
            dp[k][h]/=0.5*(dp[j][h-1]+1);
    dp[x][0]*=0.5;
    for(int i=0,j=n,k=x;i<N&&k;++i,j=k,k=f[k])
        for(int h=1;h<N;++h)
            dp[k][h]*=0.5*(dp[j][h-1]+1);
}
double query(int n){
    double ans=0;
    for(int i=1;i<N;++i)
        ans+=(dp[n][i]-dp[n][i-1])*i;
    return ans;
}
int main(){
    s=read;
    ind(dp[1]);
    while(s--)
        if(read&1)work(t,f[++t]=read);
        else printf("%.10f\n",query(read));
    return 0;
}

原文地址:https://www.cnblogs.com/SYDevil/p/12672105.html