[Probability DP] HDU 5378 Leader in the Tree land

Source: Internet
Author: User

Test instructions

Give you a tree with a 1-bit root node, we define for each subtree, the weights of the node weights are recorded as the weights of this subtree, for you to put 1~n into this tree

The number of combinations that satisfy the maximum weight is only K.

Ideas:

We can know what the probability is that each node is a subtree, and the maximum value of the root node is the number of probabilities.

Then actually the problem becomes we in n items, each item takes the probability is pi does not take the probability is 1-pi

What's the probability of you getting k items?

And then the last ride n! Just fine. The intermediate computation uses the inverse element.

Code:

#include "cstdlib" #include "CString" #include "Cmath" #include "Cstdio" #include "queue" #include "algorithm" #include " Iostream "#include" stack "using namespace std; #define LL __int64#define N 123456vector<int>edge[2234];ll sum[1234    ],dp[1234][1234];ll In[1234],mod=1000000007ll;ll Power (ll A,ll b) {ll ans=1;        while (b) {if (b&1) ans= (ans*a)%mod;        A= (a*a)%mod;    b>>=1; } return ans;    void Dfs (int x,int f) {int ans=1,lit=edge[x].size ();        for (int i=0; i<lit; i++) {int v=edge[x][i];        if (v==f) continue;        DFS (V,X);    ANS+=SUM[V];    } Sum[x]=ans; return;}    int main () {int t,cas=1;    cin>>t; for (int i=1; i<=1234; i++) In[i]=power (i,mod-2);        preprocessing inverse while (t--) {int n,k;        scanf ("%d%d", &n,&k);        for (int i=1; i<=n; i++) edge[i].clear ();            for (int i=1; i<n; i++) {int x, y;            scanf ("%d%d", &x,&y);      Edge[x].push_back (y);      Edge[y].push_back (x);        } memset (Sum,0,sizeof (sum));        DFS (a);        Memset (Dp,0,sizeof (DP));        Dp[0][0]=1; for (int i=1, i<=n; i++) {for (int j=0; j<=k; J + +) {if (j>0) dp[i][j]=                (Dp[i][j]+dp[i-1][j-1]*in[sum[i]])%mod;            Dp[i][j]= (dp[i][j]+ (dp[i-1][j]* (sum[i]-1)%mod) *in[sum[i]]%mod)%mod;        }} ll Ans=1;        for (int i=1; i<=n; i++) ans= (ans*i)%mod;        ANS*=DP[N][K];    printf ("Case #%d:%i64d\n", cas++,ans%mod); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Probability DP] HDU 5378 Leader in the Tree land

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.