/ Randle /

记录详情

Accepted


  
# 状态 耗时 内存占用
#1 Accepted 4ms 356.0 KiB
#2 Accepted 4ms 396.0 KiB
#3 Accepted 3ms 384.0 KiB
#4 Accepted 27ms 5.008 MiB
#5 Accepted 21ms 6.742 MiB
#6 Accepted 26ms 6.75 MiB
#7 Accepted 30ms 6.625 MiB
#8 Accepted 40ms 4.875 MiB
#9 Accepted 27ms 6.82 MiB
#10 Accepted 26ms 6.973 MiB

代码

#include <stdio.h>
#include <iostream>
typedef long long ll;
const int maxn = 1005;
using namespace std;
const ll mod = 1e9+7;
int n,k;
ll fact[maxn];
inline void exgcd(ll a,ll b,ll &x,ll &y) {
	if(!b) {
		x = 1,y = 0;
		return;
	}
	exgcd(b,a%b,y,x);
	y-=x*(a/b);
}
ll ni(ll a) {
	ll x,y;
	exgcd(a,mod,x,y);
	while(x<0) x+=mod;
	return x;
}
struct edge {
	ll a,b;
	ll right,left;
} edges[maxn];
ll head[maxn],nex[maxn<<1],dis[maxn<<1],q;
inline void connect(ll x,ll y) {
	nex[++q] = head[x],head[x] = q,dis[q] = y;
	nex[++q] = head[y],head[y] = q,dis[q] = x;
}
ll cloc,in[maxn],out[maxn],fa[maxn];
void dfs(ll a,ll father) {
	fa[a] = father;
	in[a] = ++cloc;
	for(ll i = head[a]; i; i = nex[i]) {
		ll to = dis[i];
		if(to!=father)
			dfs(to,a);
	}
	out[a] = cloc;
}
ll c(ll a,ll b) {
	return 1l*((1ll*fact[a]%mod*ni(fact[b]))%mod)*ni(fact[a-b])%mod;
}
ll mem[maxn][maxn];
ll solve(ll a,ll b) {
	if(mem[a][b]) return mem[a][b];
	ll ret = 0;
	for(ll i = 1; i<=max(a,b); i++) {
		if(k-i<=b&&k-i>=1&&i<=a) {
			ret  = (ret+ 1ll*c(a,i)%mod*c(b,k-i)%mod)%mod;
		}
	}
	mem[a][b] = mem[b][a] = ret;
	return ret;
}
int main() {
	cin>>n>>k;
	for(ll i  =1; i<=n-1; i++) {
		ll x,y;
		cin>>x>>y;
		edges[i].a = x,edges[i].b = y;
		connect(x,y);
	}
	fact[0] = 1;
	for(ll i = 1; i<=n; i++)fact[i] = fact[i-1]*i%mod;
	ll ans = 0;
	dfs(1,1);
	for(ll i = 1; i<=n-1; i++) {
		edge &ed = edges[i];
		if(fa[ed.a]!=ed.b) swap(ed.a,ed.b);
		ed.right = out[ed.a]-in[ed.a]+1;
		ed.left = n -ed.right;
		ans=(ans+solve(ed.right,ed.left)%mod)%mod;
	}
	cout<<ans;
	return 0;
}

信息

递交者
类型
递交
题目
月上树(原创)
题目数据
下载
语言
C++
递交时间
2017-11-07 16:02:08
评测时间
2017-11-07 19:33:20
评测机
分数
100
总耗时
211ms
峰值内存
6.973 MiB