题解

先粘上E题代码:

// Code for problem E

/*
 * by purple
 * at 12-04-09 2:19:41 PM
 */

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

#define sz(x) ((int)((x).size()))
#define out(x) printf(#x" %d\n", x)
#define rep(i,n) for(int i=0;i<(n);++i)
#define repf(i,a,b) for(int i=(a);i<=(b);++i)

const int maxn = 64;
const int maxs = 512;

double dp[maxn][maxs], tmp[maxs], son[maxs];
vector<int> g[maxn];
int n, l, s;

void dfs(int x, int f) {
    repf (j, 0, s) {
        dp[x][j] = 0.0;
    }
    dp[x][0] = 1.0;
    for (vector<int>::iterator it = g[x].begin(); it != g[x].end(); ++it) {
        if (*it != f) {
            dfs(*it, x);
            repf (i, 0, s) {
                son[i] = tmp[i] = 0.0;
            }
            repf (i, 0, s) repf (j, 0, l) {
                if (i + j <= s) {
                    son[i + j] += dp[*it][i] / (l + 1);
                }
            }
            repf (i, 0, s) repf (j, 0, s - i) {
                tmp[max(i, j)] += son[i] * dp[x][j];
            }
            repf (i, 0, s) {
                dp[x][i] = tmp[i];
            }
        }
    }
}

int main() {
    //freopen ("E.out", "w", stdout);
    
    int t, Case = 1;
    for (scanf ("%d", &t); t; --t) {
        scanf ("%d%d%d", &n, &l, &s);
        rep (i, n) {
            g[i].clear();
        }
        rep (i, n - 1) {
            int a, b;
            scanf ("%d%d", &a, &b);
            --a, --b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        dfs (0, -1);
        double res = 0.0;
        repf (i, 0, s) {
            res += dp[0][i];
        }
        printf ("Case %d: %lf\n", Case++, res);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Lyush/p/2451828.html