[2015HDU Multi-School league title]hdu5378 Leader in the Tree land

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5378

Test instructions: Give you a tree with a n-knot point. Because there is a root tree, each node can specify a subtree with its root (this is the subtree discussed later). Now we have assigned N minister from 1 to N to these nodes (one for each node), and the largest number of minister in any subtree is the leader of the subtree, asking how many different leaders you exist.

Solution:

Quote official:

The idea of probability can be used to solve this problem. Make the subtree with the node I as the subtrees tree, and set the subtree to have just sz[i] points. Then the first point is the probability of the maximum value of the subtrees tree is 1/sz[i], and the probability of not being the maximum is (sz[i]-1)/sz[i]. It is now possible to solve the probability that there are exactly k maximum values. DP[I][J] Represents a point that takes a number from 1 to I, where exactly J points are the probability of their subtree maximum. It is easy to get the following transfer equation: dp[i][j]=dp[i-1][j]* (sz[i]-1)/sz[i]+dp[i-1][j-1]/sz[i]. This dp[n][k] is the probability that all points have exactly the K-maximum value. The topic requires the number of programmes, with a total of n! The probability of multiplication is the answer. When calculating, replace the above score with the inverse element
1 /*2 * problem:hdu5378 Leader in the Tree land3 * AUTHOR:SHJWUDP4 * Created TIME:2015/8/12 Wednesday 20:17:315 * File name:1006.cpp6 * state:accepted7 * Memo: probability dp, multiplicative inverse8  */9#include <iostream>Ten#include <cstdio> One#include <vector> A#include <cstring> -#include <algorithm> -  the using namespacestd; -  - Const intmod=1e9+7; -  + structEdge { -     intu, v; + }; A  at intN, K; -Vector<edge>edges; -vector<vector<int> >G; -vector<int> siz, INV;///siz[i]*inv[i]=1 (mod 1e9+7) - voidinit () { - edges.clear (); inG.assign (n+1, vector<int> (0)); -Siz.resize (n+1); toInv.resize (n+1); + } - voidAddedge (intUintv) { the Edges.push_back (Edge) {u, v}); *G[u].push_back (Edges.size ()-1); $ }Panax Notoginseng intPow_mod (Long LongXintNintMoD) { -     Long Longres=1; the      while(n) { +         if(N &1) res=res*x%MoD; Ax=x*x%MoD; then>>=1; +     } -     returnRes; $ } $ voidDfsintUintFA) { -siz[u]=1; -      for(intI:g[u]) { theEdge & e=Edges[i]; -         if(E.V==FA)Continue;Wuyi dfs (E.V, u); thesiz[u]+=SIZ[E.V]; -     } Wu } - intMain () { About #ifndef Online_judge $Freopen ("inch","R", stdin); -     //freopen ("Out", "w", stdout); - #endif -     intT, now=0; Ascanf"%d", &T); +      while(t--) { thescanf"%d%d", &n, &k); - init (); $          for(intI=0; i<n-1; i++) { the             intA, B; thescanf"%d%d", &a, &b); the Addedge (A, b); the Addedge (b, a); -         } inDfs1, -1); the          for(intI=1; i<=n; i++) Inv[i]=pow_mod (Siz[i], mod-2, MOD);//multiplication inverse element for siz[i] thevector<vector<Long Long> > F (n+1, vector<Long Long> (n+1,0)); Aboutf[0][0]=1; the          for(intI=1; i<=n; i++) { the              for(intj=0; j<=k; J + +) { thef[i][j]+=f[i-1][j]* (siz[i]-1)%mod*inv[i]%MOD; +                 if(j>0) f[i][j]+=f[i-1][j-1]*inv[i]%MOD; -f[i][j]%=MOD; the             }Bayi         } the         Long Longans=F[n][k]; the          for(intI=1; i<=n; i++) { -ans= (ans*i)%MOD; -         } theprintf"Case #%d:", ++Now ); theprintf"%i64d\n", ans); the     } the     return 0; -}
View Code

[2015HDU Multi-School league title]hdu5378 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.