HDU 5378(Leader in Tree Land-利用機率dp),hdu5378

來源:互聯網
上載者:User

HDU 5378(Leader in Tree Land-利用機率dp),hdu5378

Leader in Tree LandTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 498    Accepted Submission(s): 208


Problem DescriptionTree land has n cities, connected by n−1 roads. You can go to any city from any city. In other words, this land is a tree. The city numbered one is the root of this tree.

There are n ministers numbered from 1 to n. You will send them to n cities, one city with one minister.

Since this is a rooted tree, each city is a root of a subtree and there are n subtrees. The leader of a subtree is the minister with maximal number in this subtree. As you can see, one minister can be the leader of several subtrees.

One day all the leaders attend a meet, you find that there are exactly k ministers. You want to know how many ways to send n ministers to each city so that there are k ministers attend the meet.

Give your answer mod 1000000007.
 
InputMultiple test cases. In the first line there is an integerT, indicating the number of test cases. For each test case, first line contains two numbersn,k. Next n−1 line describe the roads of tree land.

T=10,1≤n≤1000,1≤k≤n
 
OutputFor each test case, output one line. The output format is Case #x:ans,x is the case number,starting from 1. 
Sample Input
23 21 21 310 82 13 24 15 36 17 38 79 710 6
 
Sample Output
Case #1: 4Case #2: 316512
 
AuthorUESTC 
Source2015 Multi-University Training Contest 7 
Recommendwange2014   |   We have carefully selected several similar problems for you:  5421 5420 5419 5418 5417 


f[i,j] 表示子樹i有j個Leader 的方案數

f[i,j]=f[i-1,j]*p+f[i-1,j-1]*(1-p) (p=1/siz[i])

把機率用逆元算來確保精度,

逆元要預先處理



#include<bits/stdc++.h> using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=Next[p])#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  #define Lson (x<<1)#define Rson ((x<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (1000000007)#define MAXN (1000+10)#define MAXM (2000+10)typedef long long ll;ll mul(ll a,ll b){return (a*b)%F;}ll add(ll a,ll b){return (a+b)%F;}ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}void upd(ll &a,ll b){a=(a%F+b%F)%F;}ll f[MAXN][MAXN];int n,K;int edge[MAXM],Next[MAXM],pre[MAXM],siz=1;void addedge(int u,int v) {edge[++siz]=v;Next[siz]=pre[u]; pre[u]=siz;}void addedge2(int u,int v) {addedge(u,v); addedge(v,u); }int sz[MAXN];void dfs(int x,int fa){Forp(x) {int v=edge[p];if (v==fa) continue;dfs(v,x);sz[x]+=sz[v];}sz[x]++;}int pow2(int a,int b){if (b==0) return 1;if (b==1) return a;int t=pow2(a,b/2);t=mul(t,t);if (b&1) t=mul(t,a);return t;}int inv[MAXN];int main(){//freopen("J.in","r",stdin);Rep(i,1001) inv[i]=pow2(i,F-2);int T;cin>>T;For(kcase,T) {scanf("%d%d",&n,&K);MEM(f) siz=1; MEM(pre) MEM(Next) MEM(edge) MEM(sz)For(i,n-1){int u,v;scanf("%d%d",&u,&v);addedge2(u,v);}  dfs(1,0);f[0][0]=1;For(i,n){Rep(j,1+min(K,i))f[i][j]=add(mul(mul(f[i-1][j],sz[i]-1),inv[sz[i]]),(j>0)?mul(f[i-1][j-1],inv[sz[i]] ) : 0 );}ll ans=f[n][K];For(i,n) ans=mul(ans,i); printf("Case #%d: %I64d\n",kcase,ans);} return 0;}



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.