HDU 5378 Leader in Tree Land(2015 多校第7场 dp)
来源:互联网 发布:sql server收费吗 编辑:程序博客网 时间:2024/05/16 00:33
Leader in Tree Land
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 150 Accepted Submission(s): 54
Problem Description
Tree land has cities, connected by 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 ministers numbered from to . You will send them to cities, one city with one minister.
Since this is a rooted tree, each city is a root of a subtree and there are 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 ministers. You want to know how many ways to send ministers to each city so that there are ministers attend the meet.
Give your answer mod .
There are
Since this is a rooted tree, each city is a root of a subtree and there are
One day all the leaders attend a meet, you find that there are exactly
Give your answer mod
Input
Multiple test cases. In the first line there is an integer , indicating the number of test cases. For each test case, first line contains two numbers . Next line describe the roads of tree land.
Output
For each test case, output one line. The output format is Case # : , is the case number,starting from .
Sample Input
23 21 21 310 82 13 24 15 36 17 38 79 710 6
Sample Output
Case #1: 4Case #2: 316512
Source
2015 Multi-University Training Contest 7
解题思路:
可以用求概率的思想来解决这个问题。令以i号节点为根的子树为第i棵子树,设这颗子树恰好有sz[i]个点。那么第i个点是第i棵子树最大值的概率为1/sz[i],不是最大值的概率为(sz[i]-1)/sz[i]。现在可以求解恰好有k个最大值的概率。
令dp[i][j]表示考虑编号从1到i的点,其中恰好有j个点是其子树最大值的概率。 很容易得到如下转移方程:dp[i][j]=dp[i-1][j]*(sz[i]-1)/sz[i]+dp[i-1][j-1]/sz[i]。这样dp[n][k]就是所有点中恰好有k个最大值的概率。
题目要求的是方案数,用总数n!乘上概率就是答案。计算的时候用逆元代替上面的分数即可
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#define LL long longusing namespace std;const int MAXN = 1000 + 10;const int MOD = 1000000000 + 7;int read(){ int res = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){if(ch == '-') f *= -1; ch = getchar();} while(ch >= '0' && ch <= '9'){res = res * 10 + ch - '0'; ch = getchar();} return res;}int sz[MAXN], vis[MAXN];int n, k;LL dp[MAXN][MAXN], inv[MAXN], fac[MAXN];struct Edge{ int to, next;}edge[MAXN<<1];int tot, head[MAXN];void init(){ tot = 0; for(int i=0;i<=n;i++) { head[i] = -1; vis[i] = 0; sz[i] = 0; }}void addedge(int u, int v){ edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++;}int dfs(int u, int pre){ int ans = 1;vis[u] = 1; for(int i=head[u];i!=-1;i=edge[i].next) { int v = edge[i].to; if(v == pre) continue; if(!vis[v]) ans += dfs(v, u); } return sz[u] = ans;}LL pow_mod(LL a, LL b){ LL res = 1; while(b) { if(b & 1) res = res * a % MOD; a = a * a % MOD; b >>= 1; } return res % MOD;}int main(){ int T, kcase = 1; for(int i=1;i<MAXN;i++) inv[i] = pow_mod(i, MOD - 2); fac[0] = 1; for(int i=1;i<MAXN;i++) fac[i] = fac[i-1] * i % MOD; T = read(); while(T--) { n = read(); k = read(); int u, v; init(); for(int i=1;i<n;i++) { u = read();v = read(); addedge(u, v); addedge(v, u); } dfs(1, -1); for(int i=0;i<=n;i++) for(int j=0;j<=k;j++) dp[i][j] = 0; dp[0][0] = 1; for(int i=1;i<=n;i++) { for(int j=0;j<=k;j++) { dp[i][j] = ((dp[i-1][j] * (sz[i] - 1)) % MOD) * inv[sz[i]] % MOD + dp[i-1][j-1] * inv[sz[i]] % MOD; } } LL ans = (dp[n][k] * fac[n]) % MOD; printf("Case #%d: %I64d\n", kcase++, ans); } return 0;}
0 0
- HDU 5378 Leader in Tree Land(2015 多校第7场 dp)
- 多校第七场 1010 hdu 5378 Leader in Tree Land(概率dp)
- HDU 5378 Leader in Tree Land (概率dp,进阶)
- HDU 5378 Leader in Tree Land【树形DP】
- [概率dp] hdu 5378 Leader in Tree Land
- hdu 5378 Leader in Tree Land(dp+逆元)
- hdu 5378 Leader in Tree Land dp+逆元
- **HDU 5378 - Leader in Tree Land(概率DP)
- HDU 5378(Leader in Tree Land-利用概率dp)
- 【DP】 HDOJ 5378 Leader in Tree Land
- hdu 5378 Leader in Tree Land
- HDU 5378 Leader in Tree Land
- hdu 5378 Leader in Tree Land 2015多校联合训练赛 树形dp
- 树形DP,计数DP,概率DP(HDU 5378,Leader in Tree Land)
- Multi-University 2015 #7 J(hdu 5378 Leader in Tree Land)
- HDOJ 5378 Leader in Tree Land 概率DP
- HDU 5378 Leader in Tree Land 树形背包
- hdu5378 Leader in Tree Land(概率dp+逆元)
- 【POJ3974】【Palindrome】【Manacher裸题】
- 句子逆序
- 高并发学习笔记
- 读点大脑科学,学会变得更聪明
- 用Maven创建第一个web项目(1)
- HDU 5378 Leader in Tree Land(2015 多校第7场 dp)
- Android详细的对话框AlertDialog.Builder使用方法
- Java垃圾回收机制
- 一级指针以及二级指针
- dm8148 HDVPSS 知识简介
- hdu 2588
- 程序员屌丝的出路在哪?
- 九月十月百度人搜,阿里巴巴,腾讯华为笔试面试八十题(第331-410题)
- IOS UILabel 详解