Leader in Tree Land

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 447    Accepted Submission(s): 185

Problem Description
Tree land has n cities, connected by n−1 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 n ministers numbered from 1 to n. You will send them to n cities, one city with one minister.

Since this is a rooted tree, each city is a root of a subtree and there are n 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 k ministers. You want to know how many ways to send n ministers to each city so that there are k ministers attend the meet.

Give your answer mod $1000000007$.

Input
Multiple test cases. In the first line there is an integer T, indicating the number of test cases. For each test case, first line contains two numbers n,k. Next n−1 line describe the roads of tree land.

$T=10,1 \leq n \leq 1000,1 \leq k \leq n$

Output
For each test case, output one line. The output format is Case #x: ans, x is the case number,starting from 1.

Sample Input
2
3 2
1 2
1 3
10 8
2 1
3 2
4 1
5 3
6 1
7 3
8 7
9 7
10 6

Sample Output
Case #1: 4
Case #2: 316512

Author
UESTC

Source
2015 Multi-University Training Contest 7  1010

解题:动态规划 这位博主大牛说得非常清楚

我们用x[i],y[i]分别代表这个节点能够成为leader和不能够成为leader的概率。

son[i] 代表以i节点为根的子树的节点数。

那么$x[i] = 1/son[i],y[i] = 1-(1/son[i])$。因为这里面出现了分数,所有我们用逆元处理一下。

我们设dp[i][j]表示编号为1,2...i的节点中有j个leader的概率。

那么转移方程就是 $dp[i][j] = dp[i-1][j-1] * x[i] + dp[i-1][j] * y[i]$。

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = ;
const int maxn = ;
struct arc {
int to,next;
arc(int x = ,int y = -) {
to = x;
next = y;
}
} e[maxn<<];
int son[maxn],head[maxn],tot;
LL F[maxn] = {},x[maxn],y[maxn],dp[maxn][maxn];
void init() {
for(int i = ; i < maxn; ++i)
F[i] = F[i-]*i%mod;
}
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
LL gcd(LL a,LL b,LL &x,LL &y) { //ax + by = gcd(a,b)
if(!b) {
x = ;
y = ;
return a;
}
LL ret = gcd(b,a%b,y,x);
y -= x*(a/b);
return ret;
}
LL Inv(LL b,LL mod) { //求b % mod的逆元
LL x,y,d = gcd(b,mod,x,y);
return d == ?(x%mod + mod)%mod:-;
}
void dfs(int u,int fa) {
son[u] = ;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].to == fa) continue;
dfs(e[i].to,u);
son[u] += son[e[i].to];
}
}
int main() {
int kase,n,k,u,v,cs = ;
init();
scanf("%d",&kase);
while(kase--) {
scanf("%d%d",&n,&k);
memset(head,-,sizeof head);
tot = ;
for(int i = ; i < n; ++i) {
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(,-);
for(int i = ; i <= n; ++i) {
x[i] = Inv(son[i],mod);
y[i] = (son[i] - )*x[i]%mod;
}
memset(dp,,sizeof dp);
dp[][] = y[];
dp[][] = x[];
for(int i = ; i <= n; ++i) {
dp[i][] = dp[i-][]*y[i]%mod;
for(int j = ; j <= min(i,k); ++j) {
LL a = dp[i-][j-]*x[i]%mod;
LL b = dp[i-][j]*y[i]%mod;
dp[i][j] = (a + b)%mod;
}
}
printf("Case #%d: %I64d\n",cs++,dp[n][k]*F[n]%mod);
}
return ;
}

2015 Multi-University Training Contest 7 hdu 5378 Leader in Tree Land的更多相关文章

  1. 2015多校第7场 HDU 5378 Leader in Tree Land 概率DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:一棵n个节点的树.对其节点进行标号(1~n).求恰好存在k个节点的标号是其节点所在子树的最 ...

  2. [概率dp] hdu 5378 Leader in Tree Land

    题意: 给你一颗以1位根节点的树.我们定义对于每一个子树,节点权值最大的权值记为这个子树的权值,为你将1~n放到这个树里 满足最大权值仅仅有k个的组合数是多少. 思路: 我们能够知道以每一个节点为子树 ...

  3. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  4. 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!

    Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...

  5. 2015 Multi-University Training Contest 8 hdu 5385 The path

    The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...

  6. 2015 Multi-University Training Contest 3 hdu 5324 Boring Class

    Boring Class Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  7. 2015 Multi-University Training Contest 3 hdu 5317 RGCDQ

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  8. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  9. 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

随机推荐

  1. Qt 3D的研究(十):描边渲染(轮廓渲染)以及Silhouette Shader

    Qt 3D的研究(十):描边渲染(轮廓渲染)以及Silhouette Shader 之前写了两篇文章,介绍了我在边缘检測上面的研究.实际上.使用GPU对渲染图像进行边缘检測.前提是须要进行两遍渲染.前 ...

  2. 【HDOJ 1009】 CRB and String

    [HDOJ 1009] CRB and String 每组两个串s t 仅仅由小写字母组成 问从s能不能变成t 改变的操作为选一个字符 在后面加上一个与所选字符不同的字符 这样的操作能够做无数次 问能 ...

  3. 让ubuntu支持GBK编码AAAAA

    Eclipse 添加GBK编码 首先Windows->Preferences, 然后选择General下面的Workspace. Text file encoding选择Other GBK, 如 ...

  4. win7如何给虚拟机设置共享文件

    友情提示:设置之前先把虚拟机关掉 1. 安装vmtools 安装过的,则不需要 重新安装 如果没有安装vmware tools,点击安装(需要联网下载) ,下载完成后,打开虚拟机 点击安装,安装完毕后 ...

  5. ps -aux ,ps aux ,ps -ef 的区别

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  6. win10 + vs2017 + vcpkg —— VC++ 打包工具

    vcpkg 是微软 C++ 团队开发的在 Windows 上运行的 C/C++ 项目包管理工具,可以帮助您在 Windows 平台上获取 C 和 C++ 库. vcpkg 自身也是使用 C++ 开发的 ...

  7. 第7章 性能和可靠性模式 Failover Cluster(故障转移群集)

    上下文 您已经决定在设计或修改基础结构层时使用群集以提供高度可用的服务. 问题 您应该如何设计一个高度可用的基础结构层,来防止因单台服务器或它所运行的软件出现故障而导致的服务丢失? 影响因素 在设计高 ...

  8. wordpress插件推荐

    以下插件可以全部到后台插件中心安装,只需使用关键词搜索即可 安全插件:Wordfence Security 后台增加一道密码:Stealth Login Page 隐藏后台登录地址:WPS-Hide- ...

  9. SourceInsight3.5 Space 替换Tab

    # SourceInsight3.5 Space 替换Tab 公司要求所有的缩进都要使用空格,而不是Tab.至于使用Tab,还是Space来进行缩进,这在网上有各种各样的讨论,毕竟使用Tab可以节省很 ...

  10. 文件系统VFS数据结构(超级块 inode dentry file)(收集整理)

    Linux虚拟文件系统四大对象: 1)超级块(super block) 2)索引节点(inode) 3)目录项(dentry) 4)文件对象(file) 一个进程在对一个文件进行操作时各种对象的引用过 ...