给出一棵树,问你要让任意k个点联通的最小需要边数之和是多少。
一般的树上题目,都是以点为核心进行的,但是这题很特别,他是以边为切入点,每条边的贡献,就是C(n,k)-C(x,k)-C(y,k)x,y分别为该边两侧,总是可以区分的。
#include <iostream>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#define debug(x) //std::cerr << #x << " = " << (x) << std::endl
using namespace std;
typedef long long LL;
const int MAXN = 1e5+17;
const int MOD = 1e9+7;
vector<int > G[MAXN];
LL ans = 0,n,k,jc[MAXN],inv[MAXN];
int vis[MAXN],sum[MAXN];
LL qm(LL a,LL b)
{
LL res = 1;
while(b)
{
if(b&1) res = res*a%MOD;
a=a*a%MOD;
b>>=1;
}
return res;
}
LL cinv(LL a)
{
return qm(a,MOD-2);
}
LL comb(LL a,LL b)
{
if(a<b) return 0;
return ((jc[a]*cinv(jc[b]))%MOD*cinv(jc[a-b]))%MOD;
}
void dfs(int p)
{
vis[p] = 1;
sum[p] = 1;
debug(p);
for(auto i:G[p])
{
debug(i);
if(!vis[i])
{
dfs(i);
sum[p] += sum[i];
}
}
debug(sum[p]);
ans = ((ans+comb(n,k)-comb(sum[p],k)-comb(n-sum[p],k))%MOD+MOD)%MOD;
}
int main()
{
#ifdef noob
freopen("Input.txt", "r", stdin);
freopen("Output.txt", "w", stdout);
#endif
cin>>n>>k;
for(int i=jc[0]=1;i<=100000;++i)
{
jc[i] = jc[i-1]*i%MOD;
}
for(int i = 0; i < n-1; i++)
{
int u,v;
cin>>u>>v;
u--,v--;
G[u].push_back(v);
G[v].push_back(u);
}
ans = 0;
dfs(0);
cout<<ans<<endl;
return 0;
}