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. Linux部署ASP.NET 5 (vNext)

    原文:Linux部署ASP.NET 5 (vNext) ASP.NET 5 (vNext) Linux部署   引言 工欲善其事,必先利其器. 首先,我们先明确下以下基本概念 Linux相关 Ubun ...

  2. BZOJ 1631: [Usaco2007 Feb]Cow Party

    题目 1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 491  Solved: 362[Sub ...

  3. Python模拟登陆

    模拟人人登陆 #encoding=utf-8 import urllib2 import urllib import cookielib def renrenBrower(url,user,passw ...

  4. NYoj-街区最短路径问题

    街区最短路径问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 一个街区有非常多住户,街区的街道仅仅能为东西.南北两种方向. 住户仅仅能够沿着街道行走. 各个街道之 ...

  5. 从数据库中,绑定JQuery Accordion控件---Repeater control

    http://aspsnippets.com/Articles/jQuery-Accordion-example-in-ASPNet-using-C-and-VBNet.aspx 1. 添加JQuer ...

  6. MYSQL - php 使用 localhost 无法连接数据库

    php 使用 localhost 无法连接数据库,而使用127.0.0.1却能连接成功. 可能原因: 系统hosts文件未提供127.0.0.1到localhost的解析.解决方法(以win7系统为例 ...

  7. HDU2527:Safe Or Unsafe(哈弗曼树)

    Problem Description Javac++ 一天在看计算机的书籍的时候,看到了一个有趣的东西!每一串字符都可以被编码成一些数字来储存信息,但是不同的编码方式得到的储存空间是不一样的!并且当 ...

  8. 【转】LINUX下一款不错的网站压力测试工具webbench

    原文链接:http://blog.csdn.net/xinqingch/article/details/8618704 安装: wget http://blog.s135.com/soft/linux ...

  9. 高质量程序设计指南C/C++语言——C++/C程序设计入门

    1.在C++/C中,全局变量(extern或static)存放在程序的静态数据区中,在程序进入main()之前创建,在main()结束之后销毁,因此在我们的代码中根本没有机会初始化它们,于是语言及其实 ...

  10. iPhone开发技巧之日志保存教程

    http://mobile.51cto.com/iphone-283337.htm Objective-C开发程序的时候,有专门的日志操作类NSLog,它将指定的输出到标准的错误输出上(stderr) ...