AC代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#define MAX 1007
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;
int t,n,k,x,y;
LL dp[MAX][MAX];
LL fac[MAX];
LL rec[MAX];
int siz[MAX];
vector<int> e[MAX];
void add ( int u , int v )
{
e[u].push_back ( v );
e[v].push_back ( u );
}
void dfs ( int u , int p )
{
siz[u] = 1;
for ( int i = 0 ; i < e[u].size() ; i++ )
{
int v = e[u][i];
if ( v == p ) continue;
dfs ( v , u );
siz[u] += siz[v];
}
}
LL inv ( LL num , LL x )
{
LL ret = 1LL;
while ( x )
{
if ( x&1 )
{
ret *= num;
ret %= mod;
}
num *= num;
num %= mod;
x >>= 1;
}
return ret;
}
void pre ()
{
fac[0] = 1;
for ( LL i = 1 ; i < MAX ; i++ )
{
fac[i] = fac[i-1]*i;
fac[i] %= mod;
rec[i] = inv ( i , mod-2 );
}
}
void init ( )
{
for ( int i = 0 ; i < MAX ; i++ )
e[i].clear();
memset ( dp , 0 , sizeof ( dp ));
}
int main ( )
{
pre();
scanf ( "%d" , &t );
int cc = 1;
while ( t-- )
{
init ();
scanf ( "%d%d" , &n , &k );
for ( int i = 1 ; i < n ; i++ )
{
scanf ( "%d%d" , &x , &y );
add ( x , y );
}
dfs ( 1 , -1 );
dp[0][0] = 1LL;
for ( int i = 1 ; i <= n ; i++ )
for ( int j = 0; j <= k ; j++ )
{
dp[i][j] = dp[i-1][j]*(siz[i]-1LL)%mod*rec[siz[i]]%mod;
if ( j )
{
dp[i][j] += dp[i-1][j-1]*rec[siz[i]]%mod;
dp[i][j] %= mod;
}
}
printf ( "Case #%d: %I64d\n" , cc++ , fac[n]*dp[n][k]%mod );
}
}