Bear and Destroying Subtrees CodeForces - 643E

Bear and Destroying Subtrees

CodeForces - 643E

 

看了很久,稍微理解了一点...

代码转自czk

dp[v][h]表示以v为祖先的子树最大高度为h时的概率

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <ctime>
 7 using namespace std;
 8 
 9 
10 #define N 500020
11 #define T 66
12 #define eps 1e-8
13 
14 int n;
15 double dp[N][T];
16 int fa[N], d[N];
17 
18 
19 int main() {
20     freopen("in.txt", "r", stdin);
21     int q;
22     scanf("%d", &q);
23     n = 1;
24     for(int i = 0; i < T; ++i) dp[1][i] = 1;
25 
26     while(q--) {
27         int t, v;
28         scanf("%d%d", &t, &v);
29         if(t == 1) {
30             ++n;
31             for(int i = 0; i < T; ++i) dp[n][i] = 1;
32             fa[n] = v;
33             d[v]++;
34             double pre = dp[v][0];
35 
36             dp[v][0] = pow(0.5, d[v]);
37             int k = 1;
38             while(fa[v] && k < T) {
39                 int u = fa[v];
40                 double tmp = dp[u][k];
41                 dp[u][k] /= 0.5 + 0.5 * pre;
42                 dp[u][k] *= 0.5 + 0.5 * dp[v][k-1];
43                 pre = tmp;
44                 v = u;
45                 ++k;
46             }
47         }
48         else {
49             double ans = 0;
50             for(int i = 1; i < T; ++i) ans += i * (dp[v][i] - dp[v][i-1]);
51             printf("%.12lf\n", ans);
52         }
53     }
54     return 0;
55 }
View Code

 

posted @ 2017-10-20 18:33  yijiull  阅读(225)  评论(0编辑  收藏  举报