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. luogu2766 最长不下降子序列问题 DP 网络流

    题目大意:给定正整数序列x1,...,xn .(1)计算其最长不下降子序列的长度s.(不一定是否连续)(2)计算从给定的序列中最多可取出多少个长度为s的不下降子序列.(序列内每一个元素不可重复)(3) ...

  2. HDU3496 Watch the Movie 背包

    题目大意:给你n张电影门票,但一次只可以买m张,并且你最多可以看L分钟,接下来是n场电影,每一场电影a分钟,b价值,要求恰好看m场电影所得到的最大价值,要是看不到m场电影,输出0. 三个限制: 选电影 ...

  3. nyoj--744--蚂蚁的难题(一)

    蚂蚁的难题(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 小蚂蚁童鞋最近迷上了位运算,他感觉位运算非常神奇.不过他最近遇到了一个难题: 给定一个区间[a,b],在 ...

  4. Maven与IDEA结合

    转自:https://blog.csdn.net/zzpzheng/article/details/49474671 1. 什么是 Maven,为什么要使用 Maven 而不是 Ant Maven简单 ...

  5. Dynamic编程

    Dynamic Binding 动态绑定 Binding:解析Type,member,operation的过程. 动态绑定将Binding从编译时延迟到运行时进行. 场景 编译时,程序员知道特定的fu ...

  6. 《剑指offer 第二版》题解

    剑指Offer 按题号排序 面试题 3:数组中重复的数字 面试题 4:二维数组中的查找 面试题 5:替换空格 面试题 6:从头到尾打印链表 面试题 7:重建二叉树 面试题 8:二叉树的下一个节点 面试 ...

  7. Python 实现简单图片验证码登录

    朋友说公司要在测试环境做接口测试,登录时需要传入正确的图片的验证码,本着懒省事的原则,推荐他把测试环境的图片验证码写死,我们公司也是这么做的^_^.劝说无果/(ㄒoㄒ)/~~,只能通过 OCR 技术来 ...

  8. MacType 缺字问题【转】

  9. [翻译]开源PostgreSQL监控工具OPM

    一个好消息:九月,PostgreSQL OPM开发小组发布了开源的PostgreSQL数据库监控套件的第一个RELEASE版本OPM v2.3.PostgreSQL是先进的高级数据库,但它的一个重要的 ...

  10. spring中log4j的使用---转载

    原文链接:http://www.codeceo.com/article/log4j-usage.html 日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供 ...