###### HDU 5378 (概率dp)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 626    Accepted Submission(s): 283

Problem Description
Tree land has n cities, connected by n1 roads. You can go to any city from any city. In other words, this land is a tree. The city numbered one is the root of this tree.

There are n ministers numbered from 1 to n. You will send them to n cities, one city with one minister.

Since this is a rooted tree, each city is a root of a subtree and there are n subtrees. The leader of a subtree is the minister with maximal number in this subtree. As you can see, one minister can be the leader of several subtrees.

One day all the leaders attend a meet, you find that there are exactly k ministers. You want to know how many ways to send n ministers to each city so that there are kministers attend the meet.

Input
Multiple test cases. In the first line there is an integer T, indicating the number of test cases. For each test case, first line contains two numbers n,k. Next n1 line describe the roads of tree land.

T=10,1n1000,1kn

Output
For each test case, output one line. The output format is Case #xansx is the case number,starting from 1.

Sample Input
2 3 2 1 2 1 3 10 8 2 1 3 2 4 1 5 3 6 1 7 3 8 7 9 7 10 6

Sample Output
Case #1: 4 Case #2: 316512

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define maxn 1111
#define maxm maxn*2
#define mod 1000000007

struct node {
int v, next;
}edge[maxn];
long long dp[maxn];
long long rev[maxn];
long long f[maxn][maxn];

void add_edge (int u, int v) {
}

void dfs (int u, int fa) {
dp[u] = 1;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].v;
if (v == fa)
continue;
dfs (v, u);
dp[u] += dp[v];
}
}

long long cal (long long num) {
return (num%mod+mod)%mod;
}

long long solve () {
for (int i = 1; i <= n; i++) dp[i] = rev[dp[i]]%mod;
memset (f, 0, sizeof f);
f[1][1] = dp[1];
f[1][0] = cal (1-dp[1]);
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= k; j++) {
f[i][j] += f[i-1][j]*cal (1-dp[i])%mod;
if (j)
f[i][j] += f[i-1][j-1]*dp[i]%mod;
f[i][j] %= mod;
}
}
long long ans = 1;
for (int i = 2; i <= n; i++)
ans *= i, ans %= mod;
return (ans*f[n][k] % mod);
}

void init() {  //预处理逆元
rev[1] = 1;
for (int i = 2; i <= 1000; i++){
rev[i] = (long long)(mod - mod/i) * rev[mod % i] % mod;
}
}

int main () {
//freopen ("in.txt", "r", stdin);
init ();
int t, kase = 0;
cin >> t;
while (t--) {
cin >> n >> k;
cnt = 0;
printf ("Case #%d: ", ++kase);
for (int i = 1; i < n; i++) {
int u, v;
scanf ("%d%d", &u, &v);
}
dfs (1, 0);
printf ("%lld\n", solve ());
}
return 0;
}


#### HDU 5378 Leader in Tree Land（树形背包+组合数学）

2015-11-15 17:42:03

#### hdu 5378 概率dp 逆元

2015-08-14 00:13:17

#### 概率DP入门大集合

2013-09-21 10:51:16

#### HDU 3366 Passage (概率DP）

2016-07-26 19:29:01

#### hdu3276(概率dp)

2016-08-03 18:00:54

#### HDU4652Dice(概率DP)

2015-08-27 15:01:56

#### [ACM] hdu 5001 Walk （概率DP）

2014-09-17 17:23:32

#### [ACM] HDU 4576 Robot （概率DP,滚动数组）

2014-08-01 16:34:06

#### HDU 5378(Leader in Tree Land-利用概率dp)

2015-08-27 18:49:19

#### **HDU 5378 - Leader in Tree Land（概率DP）

2015-08-13 21:36:20

HDU 5378 (概率dp)