HDU 5378 Leader in Tree Land(2015 多校第7场 dp)

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 .

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

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

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 long
using 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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 23:15:30

HDU 5378 Leader in Tree Land(2015 多校第7场 dp)的相关文章

hdu 5378 Leader in Tree Land(dp+逆元)

题目链接:hdu 5378 Leader in Tree Land 问题可以理解成N个节点的树,有K个ministers的概率,最后乘上N!.每个节点为ministers的概率即为1 / son(以该节点为根节点的子树包含的节点个数),同样不为ministers的概率为(son-1)/son.所以没有必要考虑树的结构,直接根句子节点的个数转移dp[i][j] dp[i][j] = dp[i-1][j-1] * 1 / son[u] dp[i][j] = dp[i-1][j] * (son[u]-

HDU 5378 Leader in Tree Land

可以用求概率的思想来解决这个问题.令以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]就是所有点中恰好有

HDU 5378 Leader in Tree Land 树形背包

链接 题解来源:点击打开链接 题意: 给定n k 下面n-1行给出一棵树. 把数字1-n填到树的节点上. 填完后计算leader节点个数,若这个点是leader,则这个点上填的数>这个点的子树上填的数 问:恰好有k个leader节点的 填涂方案数. 思路: dp[i][j]表示以i点为根的子树 有恰好j个leader的方案数. 如果u 是叶子节点则 dp[u][0] = 0, dp[u][1] = 1; 如果u不是叶子节点: 先不考虑u点能否成为leader,背包一下. 然后考虑u点:若u能成为

[概率dp] hdu 5378 Leader in Tree Land

题意: 给你一颗以1位根节点的树,我们定义对于每个子树,节点权值最大的权值记为这个子树的权值,为你将1~n放到这个树里 满足最大权值只有k个的组合数是多少. 思路: 我们可以知道以每个节点为子树,且根节点权值最大的概率是多少,不是的概率是多少. 那么其实问题就变成了 我们在n个物品里面,每个物品拿的概率是pi不拿的概率是1-pi 问你拿k个物品的概率是多少 然后最后乘n!就好了.中间计算运用逆元. 代码: #include"cstdlib" #include"cstring&

HDU 5352 MZL&#39;s City(2015 多校 第5场,最小费用最大流)

  MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 291    Accepted Submission(s): 94 Problem Description MZL is an active girl who has her own country. Her big country has N cities nu

HDOJ 5378 Leader in Tree Land 概率DP

方法太屌,只能看一看了..... 可以用求概率的思想来解决这个问题.令以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].这样

[2015hdu多校联赛补题]hdu5378 Leader in Tree Land

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:给你一棵n个结点的有根树.因为是有根树,那么每个结点可以指定以它为根的子树(后面讨论的子树都是这个).现在我们把标号从1到n的n个minister派驻到这些结点上面(每个结点派驻一人),并规定任一子树中编号最大的minister 为该子树的领导,问你存在多少个不同的领导 解: 引用官方题解: 可以用求概率的思想来解决这个问题.令以i号节点为根的子树为第i棵子树,设这颗子树恰好有sz[i]

hdu 5288||2015多校联合第一场1001题

http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l<=i<=r) , that there's no j(l<=j<=r,j<>i) satisfy ai mod aj=0,now OO want to know ∑i

hdu5289||2015多校联合第一场1002贪心+RMQ

http://acm.hdu.edu.cn/showproblem.php?pid=5289 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this company, and every staff has a ability. Now, Tom is going to assign a special task to