牛客國慶集訓派對Day3 H Travel(連通塊計數)
阿新 • • 發佈:2018-12-13
題意:求對於條邊連通的個城市進行次旅遊,每一座城市恰好旅行一次有多少種方案。(每次至少旅遊一座城市)
題解:求樹分割成塊,然後全排列即可。即從條邊選擇條邊進行分割,再對個連通塊全排列。
程式碼
#include<bits/stdc++.h>
typedef long long LL;
const int N = 1e6, mod = 1e9+7;
LL fac[N];
LL pow(LL a,LL n)
{
LL res = a;
LL ans = 1;
while(n){
if(n & 1)
ans = ans * res % mod;
res = res * res % mod;
n >>= 1;
}
return ans % mod;
}
LL Inv(LL n)
{
return pow(n, mod - 2);
}
LL C(LL m, LL n)
{
if(m < 0|| m < n)
return 0;
return fac[ m] * Inv(fac[n]) % mod * Inv(fac[m - n]) % mod;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
fac[0] = 1;
for(int i = 1; i <= 100001; ++i){
fac[i] = fac[i - 1] * i % mod;
}
int n,m,T,a,b;
std::cin>>T;
while(T--){
std: :cin>>n>>m;
for(int i = 0; i < n - 1; ++i)
scanf("%d%d",&a,&b);
std::cout << C(n-1,m-1) % mod * fac[m] % mod << std::endl;
}
return 0;
}