[概率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 ...
随机推荐
- Linux Shell系列教程之(十)Shell for循环
本文是Linux Shell系列教程的第(十)篇,更多Linux Shell教程请看:Linux Shell系列教程 基本任何语言都有自己的循环语句,Shell当然也不例外,今天就为大家介绍下Shel ...
- iOS长按手势调用两次解决方法
由于以前没有很细致的研究过长按手势,所以今天使用的时候发现长按手势会调用两次响应事件. 主要原因是长按手势会分别在UIGestureRecognizerStateBegan和UIGestureReco ...
- HDU-3315 My Brute
先对于每种可能的PK情况进行判断胜负,然后求最小改动最大匹配. #include <cstdlib> #include <cstdio> #include <cstrin ...
- BZOJ3930 [CQOI2015]选数 【容斥】
题目 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选出的N个整数都求一次最大公约数,以便进一步研 ...
- 用户认证系统 django.contrib.auth模块
一 导入auth模块 from django.contrib.auth.models import User from django.contrib import auth auth模块的操作针对的就 ...
- rabbit-mq使用官方文档
http://www.rabbitmq.com/tutorials/tutorial-one-python.html
- uva 11732 (trie树)
题意:求N个字符串两两比较,共比较了多少次? #include<iostream> #include<cstring> #include<cstdio> using ...
- aoj 2226 Merry Christmas
Merry Christmas Time Limit : 8 sec, Memory Limit : 65536 KB Problem J: Merry Christmas International ...
- mybatis传入map参数,map中包含list(输入参数)
1.xml中配置: <!-- 根据条件查询满足条件的ID集合开始 --> <select id="getQuestionsIdsForExamPaper" res ...
- 标准C程序设计七---64
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...