HDU 4625: JZPTREE 第二类斯特林数 树形dp

JZPTREE

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 655360/655360 K (Java/Others)
Total Submission(s): 387    Accepted Submission(s): 166


Problem Description
There is a tree,there is a life.Can you solve this problem about tree?
Here we have a tree which has n vertices.
We define dist(u, v) as the number of edges on the path from u to v.
And for each vertices u , define E u =
Give you the tree and k.
Print E i for every vertices(from 1 to n).(mod 10007 for convenience).
 

Input
The first line contains integer T(1 T 5),denote the number of the test cases.
For each test cases,the first line contains two integers n,k(1<=n<=50000, 1<=k<=500).
Then next n-1 lines,each lines contains two integer a,b(1<=a, b<=n),denote there is an edge between a, b.
 

Output
For each test cases,print n lines,the i-th line contains E i.
 

Sample Input
 
 
1 5 3 1 2 2 3 3 4 4 5
 

Sample Output
 
 
100 37 18 37 100
Hint
Thank oimaster for this problem :).
 

Author
WJMZBMR


妙题啊

不过调了好几h 太痛苦了

为什么呢 写法是跑 K 遍树dp

谁tm告诉我 20倍常数是咋来的 要哭了

转载自http://blog.csdn.net/dwylkz/article/details/9749827



#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=50100,M=510,mod=10007;

int last[N],ecnt;
struct EDGE{int to,nt;}e[N<<1];
inline void add(int u,int v)
{e[++ecnt]=(EDGE){v,last[u]};last[u]=ecnt;}

int fac[M],S[M][M];

void initial()
{
	register int i,j;
	fac[0]=1;
	for(i=1;i<M;++i)
		fac[i]=i*fac[i-1]%mod;
	S[0][0]=1;
	for(i=1;i<M;++i)
		for(j=1;j<=i;++j)
			S[i][j]=(j*S[i-1][j]+S[i-1][j-1])%mod;
}

void init()
{
	ecnt=0;
	memset(last,0,sizeof(last));
}

int n,K;
int f[N][M],g[N][M];

void dfs_1(int u,int pre)
{
	memset(g[u],0,sizeof(g[u]));
	g[u][0]=1;
	for(int i=last[u],v,j;i;i=e[i].nt)
		if((v=e[i].to)!=pre)
		{
			dfs_1(v,u);
			g[u][0]+=g[v][0];
			for(j=1;j<=K;++j)
				g[u][j]+=g[v][j]+g[v][j-1];
		}
	for(int i=0;i<=K;++i)
		g[u][i]%=mod;
}

void dfs_2(int u,int pre)
{
	for(int i=last[u],v,j;i;i=e[i].nt)
		if((v=e[i].to)!=pre)
		{
			f[v][1]=(f[u][1]+f[u][0]-(g[v][0]<<1)%mod+mod)%mod;
			for(j=2;j<=K;++j)
				f[v][j]=((f[u][j]+f[u][j-1]-(g[v][j-1]<<1)-g[v][j-2])%mod+mod)%mod;
			dfs_2(v,u);
		}
}

int E[N];

int main()
{
	initial();
	register int T=read(),i,j,u,v;
	while(T--)
	{
		init();
		n=read(),K=read();
		for(i=1;i<n;++i)
			u=read(),v=read(),
			add(u,v),add(v,u);
		dfs_1(1,0);
		for(i=0;i<=K;++i)
			f[1][i]=g[1][i];
		for(i=1;i<=n;++i)
			f[i][0]=n;
		dfs_2(1,0);
		for(i=1;i<=n;++i)
		{
			E[i]=0;
			for(j=1;j<=K;++j)
				E[i]+=S[K][j]*fac[j]%mod*f[i][j]%mod;
			E[i]%=mod;
		}
		for(i=1;i<=n;++i)
			print(E[i]),putchar('\n');
	}
	return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值