Bubble Cup 11 - Finals [Online Mirror, Div. 1] D. Interstellar battle bfs序+树状数组

Description
给出一棵树以及树上的点消失的概率
Q次修改
每次修改一个点消失的概率
问每次修改之后当前树剩下的连通块个数的期望


Sample Input
5
0.50 0.29 0.49 0.95 0.83
2 3
0 3
3 4
2 1
3
4 0.66
1 0.69
0 0.36


Sample Output
1.68040
1.48440
1.61740


OZY太强了%%%
对于一个图的期望连通块数其实就是点数-边数,
那么它的期望连通块数即为:E(a)-E(b)
E(a)表示点的期望个数,这很好计算,所有点存活概率相加即可。
E(b)表示边的期望条数,对于每条边它存活的期望为:px*py,x,y为它连接的两个点。
那么对于每一个点,每次修改时,需修改它下一层的子节点,注意他们的层数相同,即可搞个bfs序,用树状数组维护。


#include <set>
#include <queue>
#include <cstdio>
#include <cstring>

using namespace std;
typedef long long LL;
int _min(int x, int y) {return x < y ? x : y;}
int _max(int x, int y) {return x > y ? x : y;}
int read() {
	int s = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
	return s * f;
}

struct edge {
	int x, y, next;
} e[210000]; int len, last[110000];
int n, id, deg[110000], tt[110000], L[110000], R[110000];
double p[110000], s[110000];
int fa[110000];
queue<int> q;

void ins(int x, int y) {
	e[++len].x = x, e[len].y = y;
	e[len].next = last[x], last[x] = len;
}

void bfs() {
	q.push(1); tt[1] = 1; id = 1;
	memset(R, 0, sizeof(R));
	memset(L, 63, sizeof(L));
	while(!q.empty()) {
		int x = q.front(); q.pop();
		for(int k = last[x]; k; k = e[k].next) {
			int y = e[k].y;
			if(y != fa[x]) {
				tt[y] = ++id;
				fa[y] = x;
				L[x] = _min(L[x], id);
				R[x] = _max(R[x], id);
				q.push(y);
			}
		}
	}
}

int lowbit(int x) {return x & -x;}
void change(int x, double c) {for(int i = x; i <= n; i += lowbit(i)) s[i] += c;}
double getsum(int x) {double ans = 0; for(int i = x; i; i -= lowbit(i)) ans += s[i]; return ans;}

int main() {
	n = read();
	double V = 0;
	for(int i = 1; i <= n; i++) {
		scanf("%lf", &p[i]);
		p[i] = 1.0 - p[i];
		V += p[i];
	} double E = 0;
	for(int i = 1; i < n; i++) {
		int x = read(), y = read();
		x++, y++;
		E += p[x] * p[y];
		ins(x, y), ins(y, x);
		deg[x]++, deg[y]++;
	} bfs();
	for(int i = 1; i <= n; i++) {
		if(R[i] == 0) R[i] = tt[i];
		if(L[i] > n) L[i] = tt[i] + 1;
	}
	for(int i = 1; i <= n; i++) change(tt[i], p[i]);
	int q = read();
	for(int i = 1; i <= q; i++) {
		int x = read(); double yy; x++;
		scanf("%lf", &yy); yy = 1.0 - yy;
		double u = p[x];
		V -= p[x], p[x] = yy, V += p[x];
		if(x != 1) {
			E -= p[fa[x]] * u;
			E += p[fa[x]] * yy;
			double sum = getsum(R[x]) - getsum(L[x] - 1);
			E -= sum * u;
			E += sum * yy;
		} else {
			double sum = getsum(R[x]) - getsum(L[x] - 1);
			E -= sum * u;
			E += sum * yy;
		} change(tt[x], yy - u);
		printf("%.5lf\n", V - E);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值