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

49 篇文章 0 订阅
36 篇文章 4 订阅

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值