/ Randle /

记录详情

Runtime Error


  
# 状态 耗时 内存占用
#1 Wrong Answer 4ms 256.0 KiB
#2 Wrong Answer 4ms 256.0 KiB
#3 Wrong Answer 4ms 372.0 KiB
#4 Runtime Error 21ms 8.25 MiB
#5 Wrong Answer 5ms 2.344 MiB
#6 Runtime Error 21ms 8.25 MiB
#7 Runtime Error 20ms 8.367 MiB
#8 Runtime Error 24ms 8.25 MiB
#9 Runtime Error 29ms 8.375 MiB
#10 Runtime Error 21ms 8.375 MiB

代码

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

信息

递交者
类型
递交
题目
月上树(原创)
题目数据
下载
语言
C++
递交时间
2017-11-07 12:28:56
评测时间
2017-11-07 12:28:57
评测机
分数
0
总耗时
158ms
峰值内存
8.375 MiB