Codeforces 1045D Interstellar battle

Interstellar battle

China Unicom for the contribution of each block we put it on the lowest point of the depth to the top.

Then a point is contributing its own father was destroyed and not destroyed.

Then all probabilities add up to the expectations.

#include<bits/stdc++.h>
using namespace std;

const int N = (int)1e5 + 7;

int n;
double p[N];
double ans;
double sum[N];
int pa[N];
vector<int> G[N];

void dfs(int u, int fa) {
    pa[u] = fa;
    ans += p[fa] * (1 - p[u]);
    for(auto v : G[u]) {
        if(v == fa) continue;
        dfs(v, u);
        sum[u] += 1 - p[v];
    }
}

int main() {
    p[0] = 1;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%lf", &p[i]);
    for(int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        u++; v++;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1, 0);
    int q; scanf("%d", &q);
    while(q--) {
        int u; double x;
        scanf("%d%lf", &u, &x);
        u++;
        if(pa[u]) sum[pa[u]] -= 1 - p[u];
        ans -= (1 - p[u]) * p[pa[u]];
        ans -= p[u] * sum[u];
        p[u] = x;
        if(pa[u]) sum[pa[u]] += 1 - p[u];
        ans += (1 - p[u]) * p[pa[u]];
        ans += p[u] * sum[u];
        printf("%.12f\n", ans);
    }
    return 0;
}

/*
*/

 

Guess you like

Origin www.cnblogs.com/CJLHY/p/11687528.html