2021.07.19【NOIP提高B组】模拟 概率充电器

11 篇文章 0 订阅
5 篇文章 0 订阅

在这里插入图片描述

思路:

首先拓扑一遍,然后树上跑两次,一次求向下传,一次求向上传

c o d e code code

#include<iostream>
#include<cstdio>
#include<queue>

using namespace std;

int n, deg[500100], fa[500100];
int head[500100], tot;
struct node
{
	int to, next;
	double w;
}b[5001000];
double f[500100], g[500100];

void add(int x, int y, double w)
{
	b[++tot]=(node){y, head[x], w};
	head[x]=tot;
}

double merge_(double &x, double y)
{
	return x+y-x*y;
}

void bfs()
{
	queue<int> q;
	q.push(1);
	while(!q.empty())
	{
		int x=q.front();
		q.pop();
		for(int i=head[x]; i; i=b[i].next)
		{
			int y=b[i].to;
			if(y!=fa[x])
			{
				fa[y]=x;
				deg[y]--;
				q.push(y);
			}
		}
	}
}

void bfs1()
{
	queue<int> q;
	for(int i=1; i<=n; i++)
		if(!deg[i])
			q.push(i);
	while(!q.empty())
	{
		int x=q.front();
		q.pop();
		deg[fa[x]]--;
		if(deg[fa[x]]==0)
			q.push(fa[x]);
		for(int i=head[x]; i; i=b[i].next)
		{
			int y=b[i].to;
			if(fa[x]!=y)
			{
				g[y]=f[y]*b[i].w;
				f[x]=merge_(f[x], g[y]);
			}
		}
	}
}

void bfs2()
{
	queue<int> q;
	q.push(1);
	while(!q.empty())
	{
		int x=q.front();
		q.pop();
		for(int i=head[x]; i; i=b[i].next)
		{
			int y=b[i].to;
			if(y!=fa[x])
			{
				if(g[y]<1.0)
					f[y]=merge_(f[y], (f[x]-g[y])/(1-g[y])*b[i].w);
				q.push(y);
			}
		}
	}
}

int main()
{
	freopen("charger.in", "r", stdin);
	freopen("charger.out", "w", stdout);
	scanf("%d", &n);
	for(int i=1; i<n; i++)
	{
		int x, y, w;
		scanf("%d%d%d", &x, &y, &w);
		add(x, y, w*1.0/100.0);
		add(y, x, w*1.0/100.0);
		deg[x]++;
		deg[y]++;
	}
	for(int i=1; i<=n; i++)
	{
		scanf("%lf", &f[i]);
		f[i]/=100.0;
	}
	bfs();
	bfs1();
	bfs2();
	double ans=0;
	for(int i=1; i<=n; i++)
		ans+=f[i];
	printf("%.6lf", ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值