hdu 5036 Explosion (期望+传递闭包)

探讨了一个关于游戏内使用炸弹开启门的游戏策略问题,利用概率论和组合数学原理,计算了玩家在不同房间配置下使用炸弹开启所有门的期望次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Explosion

Problem Description

Everyone knows Matt enjoys playing games very much. Now, he is playing such a game. There are N rooms, each with one door. There are some keys(could be none) in each room corresponding to some doors among these N doors. Every key can open only one door. Matt has some bombs, each of which can destroy a door. He will uniformly choose a door that can not be opened with the keys in his hand to destroy when there are no doors that can be opened with keys in his hand. Now, he wants to ask you, what is the expected number of bombs he will use to open or destroy all the doors. Rooms are numbered from 1 to N.

Input

The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow.

In the first line of each test case, there is an integer N (N<=1000) indicating the number of rooms.

The following N lines corresponde to the rooms from 1 to N. Each line begins with an integer k (0<=k<=N) indicating the number of keys behind the door. Then k integers follow corresponding to the rooms these keys can open.

Output

For each test case, output one line “Case #x: y”, where x is the case number (starting from 1), y is the answer which should be rounded to 5 decimal places.

Sample Input



1 2 
1 3 
1 1 



0

Sample Output

Case #1: 1.00000 
Case #2: 3.00000

题意:n个房间,每个房间都有若干个钥匙打开其他的门,如果手上没有钥匙可以选择等概率随机选择一个门炸开,求用炸弹数的期望。

打开所有门要炸的期望次数应该等于对每个门使用炸弹的期望次数之和,而不是打开每个门需要炸蛋的期望数量的和
打开一个门需要炸的期望次数是n/k,而对一个门使用炸弹的期望次数等于1/k,因为有k个门可以用来间接打开一个门
这其中只有通过打开这个门自己来“间接”打开这个门,是需要用1个炸弹的,这种情况的概率是1/k,因为一个门最后一定要被打开
,显然没必要对一个门用两次以上炸弹,所以用0次炸弹的概率是(1-1/k),对这个门使用炸弹的期望次数也就等于1*(1/k)+0*(1-1/k)=1/k
全加起来就得到答案了,打开所有门要炸的期望次数不是等于打开每个门需要炸的期望次数的和的,k应该指的是打开后能打开这个门的门的个数
ki=|Si|,当打开Si后任何一个门后,门i就会被打开,显然i∈Si,从S中随机取取到i的概率为1/k,也就是说,不借助其他门
i被炸开,这种情况发生的概率是1/k,这种情况下 只会有一个炸弹被用来炸i,其他情况下不需要用炸弹来炸i,根据期望的定义就可以得到被用来炸i的炸弹的个数的期望值,ki=|Si|,根据期望的定义就可以得到被用来炸i的炸弹的个数的期望值,n/k是单个门被打开需要炸的期望次数
直接加起来不满足期望的线性性质。这其实就是求了对于特定的门使用炸弹次数的期望值
知道了要求的是这个东西 就比较好算了

这里求传递闭包要bitset优化一下,for的循环顺序不能变

#include <iostream>
#include <cstring>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
const int maxn = 1e3 + 5;
bitset<maxn> v[maxn];
int main()
{
    int t, ca = 1, n, k, x;
    cin >> t;
    while(t--)
    {
        scanf("%d", &n);
        for (int i = 0; i <= n; i++)
        {
            v[i].reset();
            v[i][i] = true;
        }
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &k);
            for(int j = 1; j <= k; j++)
            {
                scanf("%d", &x);
                v[i][x] = 1;
            }
        }
        for(int i = 1; i <= n; i++)  //不能变
            for(int j = 1; j <= n; j++)
                if(v[j][i])
                    v[j] |= v[i];
        double ans = 0;
        for(int i = 1; i <= n; i++)
        {
            int cnt = 0;
            for(int j = 1; j <= n; j++)
            {
                if(v[j][i]) cnt++;  //不能变
            }
            ans += 1.0 / cnt;
        }
        printf("Case #%d: %.05f\n", ca++, ans);
    }
    return 0;
}




### CodeForces 平台上关于传递闭包的概念及其实现 #### 传递闭包定义 在一个有向图 \( G=(V,E) \) 中,如果存在一条从顶点 \( u \) 到顶点 \( v \) 的路径,则认为 \( u \) 可达于 \( v \)[^1]。传递闭包是指通过某种方式使得所有可达关系显式表示出来的新图。 #### Floyd-Warshall算法用于计算传递闭包 Floyd-Warshall是一种经典的多源最短路算法,在此可以用来构建传递闭包矩阵。该方法基于动态规划的思想,时间复杂度为\( O(n^3) \),其中\( n \) 是节点数量。具体来说: ```cpp const int MAXN = 105; bool graph[MAXN][MAXN]; // 初始化为false void floyd_warshall(int n){ for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) graph[i][j] |= (graph[i][k] && graph[k][j]); } ``` 这段代码遍历所有的中间结点`k`,并更新每一对结点之间的连通状态。当发现可以通过某个中间结点到达另一结点时,就标记相应的位为真。 #### 应用场景——二分图匹配中的最长反链寻找 在某些题目中,比如Codeforces 590E Birthday问题里提到的方法之一就是先求解传递闭包再利用二分图的最大独立集来找到最长反链[^4]。这通常涉及到将原图转换成其对应的传递闭包版本,之后按照特定策略进行匹配操作找出符合条件的结果集合。 #### 使用Bitset优化枚举过程 考虑到效率问题,在处理大规模数据结构下的传递闭包运算时可采用bitset来进行快速逻辑运算。例如,在HDU 6496题目的分析部分描述了一种使用bitset加速交点判断的技术[^2]。这种方法能够显著减少不必要的重复计算量,从而提高整体性能表现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值