[概率dp] hdu 5378 Leader in Tree Land
题意:
给你一颗以1位根节点的树。我们定义对于每一个子树,节点权值最大的权值记为这个子树的权值,为你将1~n放到这个树里
满足最大权值仅仅有k个的组合数是多少。
思路:
我们能够知道以每一个节点为子树。且根节点权值最大的概率是多少,不是的概率是多少。
那么事实上问题就变成了 我们在n个物品里面,每一个物品拿的概率是pi不拿的概率是1-pi
问你拿k个物品的概率是多少
然后最后乘n!就好了。中间计算运用逆元。
代码:
#include"cstdlib"
#include"cstring"
#include"cmath"
#include"cstdio"
#include"queue"
#include"algorithm"
#include"iostream"
#include"stack"
using namespace std;
#define ll __int64
#define N 123456
vector<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); //预处理逆元
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(1,1);
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;
}
[概率dp] hdu 5378 Leader in Tree Land的更多相关文章
- 2015 Multi-University Training Contest 7 hdu 5378 Leader in Tree Land
Leader in Tree Land Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- 2015多校第7场 HDU 5378 Leader in Tree Land 概率DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:一棵n个节点的树.对其节点进行标号(1~n).求恰好存在k个节点的标号是其节点所在子树的最 ...
- [2015hdu多校联赛补题]hdu5378 Leader in Tree Land
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:给你一棵n个结点的有根树.因为是有根树,那么每个结点可以指定以它为根的子树(后面讨论的子树 ...
- 概率DP hdu 3366 .
题意:一个人被困在一个城堡里,面前有n条路,他自己有m百万元,选择每一条路都有p概率通过,q概率遇到士兵,1-p-q概率道路不通:遇到士兵的话需要上交1百万,如果不够钱,则被杀死,问的是最优情况下多少 ...
- HDU 5378 树上的概率DP Leader in Tree Land
官方题解: 可以用求概率的思想来解决这个问题.令以i号节点为根的子树为第i棵子树,设这颗子树恰好有sz[i]个点.那么第i个点是第i棵子树最大值的概率为1/sz[i],不是最大值的概率为(sz[i]- ...
- 概率dp HDU 3853
H - LOOPS Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ci ...
- 概率dp HDU 4405
Aeroplane chess Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- 概率DP HDU 4586 play the dice
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4586 解题思路: 只考虑第一次,获得的金币的平均值为sum/n.sum为所有色子的面的金币值相加. ...
- HDU 3076:ssworld VS DDD(概率DP)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3076 ssworld VS DDD Problem Description One day, s ...
随机推荐
- mybatis分页方式对比
mybatis有两种分页方法(转自:http://blog.csdn.net/leozhou13/article/details/50394242) 1.内存分页,也就是假分页.本质是查出所有的数据然 ...
- volatile的用法
在再有人问你Java内存模型是什么,就把这篇文章发给他中我们曾经介绍过,Java语言为了解决并发编程中存在的原子性.可见性和有序性问题,提供了一系列和并发处理相关的关键字,比如synchronized ...
- [暑假集训--数位dp]hdu5787 K-wolf Number
Alice thinks an integer x is a K-wolf number, if every K adjacent digits in decimal representation o ...
- Linux System Programming 学习笔记(七) 线程
1. Threading is the creation and management of multiple units of execution within a single process 二 ...
- 過充保護警告訊息 over charging protection,Battery over voltage protection, warning message
Definition: over charging protection.battery over voltage protection, 是一種 battery 保護機制, 避免 battery 充 ...
- js库-AngularJS
我是一个不思进取的前端. 我想按部就班的工作. 我想得过且过. 老天呀!你咋又逼迫我学习对于我来说的新知识呢!!!!!!!我想哭呀!!!!! 在某个代码项目中我看到了{{??}}这么个标记!我的神呀! ...
- FFT题集
FFT学习参考这两篇博客,很详细,结合这看,互补. 博客一 博客二 很大一部分题目需要构造多项式相乘来进行计数问题. 1. HDU 1402 A * B Problem Plus 把A和B分别当作多项 ...
- NYOJ90 整数划分(经典递归和dp)
整数划分 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 将正整数n表示成一系列正整数之和:n=n1+n2+…+nk, 其中n1≥n2≥…≥nk≥1,k≥1. 正 ...
- 计蒜客 UCloud 的安全秘钥(随机化+Hash)
题目链接 UCloud 的安全秘钥 对于简单的版本,我们直接枚举每个子序列,然后sort一下判断是否完全一样即可. #include <bits/stdc++.h> using names ...
- 计蒜客 微软大楼设计方案(RMQ)
题目链接 微软大楼设计方案 中文题就不说题意了~ 首先是简单版本 满足$1 <= n, m <= 50$ 那么设$c[i][j]$为从第$i$幢楼到第$j$幢楼的最低的那幢楼的高度 计算两 ...