hdu 4219 Randomization?

本文介绍了一种使用概率动态规划(DP)解决特定树形结构问题的方法。通过定义状态dp[u][i]为子树u高度为i且任意两节点间距离不超过S的概率,结合加法与乘法原理进行状态转移,最终求得根节点满足条件的概率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

What a disgusting tree type probability dp!

But it cant annoy me anymore hoho :).

dp[u][i] as the probability that (1) The height of subtree u is i (2)  The distance between any two nodes do not exceed S;

To calculate dp[u][i], all we have to do is that for each son v of u, we combine the subtree v to current subtree u iff any path between subtree u and subtree v do not exceed S.

As to maintain property (2), we do some classification below:

Define: sum[u][i] = dp[u][0] + .... dp[u][i]; (according to addition principle)

Then according to addition principle and multiplication principle:

i + i < = S: dp[u][i] = sum[u][i] * sum[v][i] - sum[u][i - 1] * sum[v][i - 1]; 

i + i > S:   j = S - i => j < i, dp[u][i] = sum[u][i] * sum[v][j] - sum[u][i - 1] * sum[v][j] + sum[v][i] * sum[u][j] - sum[v][i - 1] * sum[u][j]

We do the substaction because the subtrahend against the property (1).

Then after DFS once , sum[root][s] will be the result.

Last but not less,  to keep your mind clear.

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
#include <queue>
#include <stack>
using namespace std;
#define MS(c, a) memset(c, a, sizeof c)
#define Rep(c, a, b) for (int c = (a); c < (b); c++)
#define Nre(c, a, b) for (int c = (a); c > (b); c--)
#define MAXN (1024)

struct Edge
{
	int v, next;
};
vector<Edge> E; 
int L[MAXN];
void G_ini()
{
	E.clear();
	MS(L, -1);
}
void AE(int u, int v)
{
	Edge te = {v, L[u]};
	L[u] = E.size();
	E.push_back(te);
}

int n, l, s;
double dp[MAXN][MAXN];

void DFS(int u, int fa)
{
	MS(dp[u], 0);
	dp[u][0] = 1;
	for (int i = L[u]; i != -1; i = E[i].next)
	{
		int v = E[i].v;
		if (v != fa)
		{
			Rep(j, 1, s + 1) dp[u][j] += dp[u][j - 1];
			DFS(v, u);
			Nre(j, s, -1) if (!j) //Combine node subtree v to current subtree u and maintain property (1) (2)
				dp[u][j] = dp[u][j] * dp[v][j];
			else if (j + j <= s)
				dp[u][j] = dp[u][j] * dp[v][j] - dp[u][j - 1] * dp[v][j - 1];
			else
			{
				int k = s - j;
				dp[u][j] = dp[u][j] * dp[v][k] - dp[u][j - 1] * dp[v][k];
				dp[u][j] += dp[u][k] * dp[v][j] - dp[u][k] * dp[v][j - 1];
			}
		}
	}
	if (fa != -1) //Get the dp[u][i] in advance
	{
		Nre(i, s, - 1) Rep(j, max(0, i - l), i)
			dp[u][i] += dp[u][j];
		Rep(i, 0, s + 1) dp[u][i] /= 1 + l;
	}
	Rep(i, 1, s + 1) dp[u][i] += dp[u][i - 1]; //Pretreat the sum[u][i]
}

int main()
{
	int T, u, v;
	scanf("%d", &T);
	Rep(Cas, 1, T + 1)
	{
		scanf("%d%d%d", &n, &l, &s);
		G_ini();
		Rep(i, 0, n - 1)
		{
			scanf("%d%d", &u, &v);
			AE(u, v);
			AE(v, u);
		}
		DFS(1, -1);
		printf("Case %d: %.6lf\n", Cas, dp[1][s]);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值