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;
}