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
2
3
1 2
1 3
1 1
3
0
0
0
 
Sample Output
Case #1: 1.00000
Case #2: 3.00000
 
Source

题意:n个房间,每一个房间都有若干个钥匙打开其它的门,假设手上没有钥匙能够选择等概率随机选择一个门炸开。求用炸弹数的期望。

思路:每一个房间期望都是可加的。

单独考虑一个房间,假设有k个房间被炸开都会导致这个房间被打开。

那么炸一次这个房间被打开的概率即为kn。也就意味着为了把这个房间打开的期望炸的次数为nk。所有加起来后除以n就可以。bitset优化。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
using namespace std;
const int maxn = 1005; bitset<maxn> a[maxn]; int main() {
int t, cas = 1;
int n;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
a[i].reset();
a[i][i] = 1;
} int c, x;
for (int i = 0; i < n; i++) {
scanf("%d", &c);
while (c--) {
scanf("%d", &x);
a[i][--x] = 1;
}
} for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (a[j][i])
a[j] |= a[i]; double ans = 0;
for (int i = 0; i < n; i++) {
c = 0;
for (int j = 0; j < n; j++)
if (a[j][i])
c++;
ans += 1.0 / c;
} printf("Case #%d: %.5lf\n",cas++,ans);
}
return 0;
}

HDU - 5036 Explosion的更多相关文章

  1. hdu 5036 Explosion bitset优化floyd

    http://acm.hdu.edu.cn/showproblem.php?pid=5036 题意就是给定一副有向图,现在需要走遍这n个顶点,一开始出发的顶点是这n个之中的随便一个. 如果走了1,那么 ...

  2. hdu 5036 Explosion(概率期望+bitset)

    Problem Description Everyone knows Matt enjoys playing games very much. Now, he to N. Input The firs ...

  3. HDU 5036 Explosion (传递闭包+bitset优化)

    <题目链接> 题目大意: 一个人要打开或者用炸弹砸开所有的门,每个门后面有一些钥匙,一个钥匙对应一个门,告诉每个门里面有哪些门的钥匙.如果要打开所有的门,问需要用的炸弹数量为多少. 解题分 ...

  4. hdu 5036 概率+bitset

    http://acm.hdu.edu.cn/showproblem.php?pid=5036 n个房间每个房间里面有一把或多把钥匙可以打开其他的门.如果手上没有钥匙可以选择等概率随机选择一个门炸开,求 ...

  5. HDU - 5036 Operation the Sequence

    Problem Description You have an array consisting of n integers: a1=1,a2=2,a3=3,-,an=n. Then give you ...

  6. Hdu 5036-Explosion 传递闭包,bitset,期望/概率

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5036 Explosion Time Limit: 6000/3000 MS (Java/Others)   ...

  7. bitset常用用法&&简单题分析

    Preface bitset,还是一个比较好用的STL,可以给一些题目做到神奇的常数优化(\(O(\frac{原来的复杂度}{机器的位数(32位or64位)})\)) 关于一些具体的函数等内容可以参考 ...

  8. hdu 4666:Hyperspace(最远曼哈顿距离 + STL使用)

    Hyperspace Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  9. (KMP 水)Wow! Such Doge! -- hdu -- 4847

    http://acm.hdu.edu.cn/showproblem.php?pid=4847 Wow! Such Doge! Time Limit:1000MS     Memory Limit:32 ...

随机推荐

  1. Ruby学习: 类的定义和实例变量

    ruby是完全面向对象的,所有的数据都是对象,没有独立在类外的方法,所有的方法都在类中定义的. 一.类的定义语法 类的定义以 class 关键字开头,后面跟类名,以 end标识符结尾. 类中的方法以 ...

  2. C++ Primer 学习笔记_87_用于大型程序的工具 --异常处理

    用于大型程序的工具 --异常处理 引言: C++语言包括的一些特征在问题比較复杂,非个人所能管理时最为实用.如:异常处理.命名空间和多重继承. 相对于小的程序猿团队所能开发的系统需求而言,大规模编程[ ...

  3. ThinkPHP - 关联模型 - 一对多

    使用之前,先引入文件夹,否则相应的功能不能实现. 如果对thinkPHP不精通,使用或开发的时候,最好直接使用完成版本的ThinkPHP. 关系模型定义: <?php /** * 继承自 Rel ...

  4. Activity中Menu相关的几个方法的调用时机

    用于创建菜单的常用的方法有如下两种: 1.onCreateOptionsMenu(Menu menu) 2.onPrepareOptionsMenu(Menu menu) MyDiaryActivit ...

  5. Main方法的执行过程(转)

    要运行一个 main 方法 , 首先要知道 main 方法所在的 Class, 在命令行中指定这个 Class 名 Class Lava{ Private int speed = 4; Void fl ...

  6. 期权put和call

    今天和朋友谈到了FB的期权操作问题,保证一个固定收益或者得到一个以低价买入FB的机会. 首先说下基本概念:call:是指买权put :是指卖权但是这两种期权又分别对应了long和short的两种操作, ...

  7. csu1306: Manor

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1306 解题思路:唬人的水题,只要按照他的意思打,就能过,不过,数组最好开大点.用到优先队列,也可以 ...

  8. 放开chrome,微度新标签页 删除

    来自VIc_:http://blog.csdn.net/Vic___ 本人只是提供方法建议,没有任何商业欲望. 欢迎转载 有一天,突然,我的标签栏,被改变了,这是怎么了,我的书签栏 我是一个镇定的人, ...

  9. Android动态设定GridView的高度,固定column,实现高度自适应

    动态设定GridView的高度,固定column,根据gridview中的item个数设定高度: 调用以下方法: public static void setListViewHeightBasedOn ...

  10. 设置不输入密码ssh登录

    在/etc/hosts文件下加入: 192.168.1.60 u60 #设置u60为主机名 在每个节点上创建RSA秘钥: # ssh-keygen -t rsa # 一直按确定键即可 # touch ...