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. BZOJ 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课

    题目 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课 Time Limit: 5 Sec  Memory Limit: 64 MB Description 考虑一 ...

  2. java学习之xml

    xml的处理有两种方式dom和Sax 其中dom有3套api ,分别是dom和jdom和dom4j package com.gh.xml; import java.io.File; import ja ...

  3. 小程序员在android移动应用上的赚钱经历

    先说说我自己吧,二线城市(以外包为主)的小程序员,工作多年了,月收入5-6K.主要从事asp.net web网站开发,java,c++,php,ruby都懂一些,属于那种对问题不求甚解型,爱好电脑游戏 ...

  4. myeclipse设置凝视

    Window-perferences--java--Code Style--Code Templates--Commments 类凝视:Types /** *@desc *@author haozk ...

  5. Codeforces 455B A Lot of Games(字典树+博弈)

    题目连接: Codeforces 455B A Lot of Games 题目大意:给定n.表示字符串集合. 给定k,表示进行了k次游戏,然后是n个字符串.每局開始.字符串为空串,然后两人轮流在末尾追 ...

  6. 中国A股市场缘何遭遇9连跌?

    端午长假刚过,当投资者对“红六月”预期信心满满的时候,A股市场却遭遇了诡异的跌势,截止6月17日收盘,A股出现了罕见的“9连跌”.不仅上证综指失守2200点年线整数位,深证成指跌幅则高达3%以上,而且 ...

  7. C# 中根据datetime的值来计算属于本年的第几周,类似delphi中的weekoftheyear功能

    /// <summary> /// 获得今天是今年的第几周 /// </summary> /// <param name="year">< ...

  8. if else配对问题

    else语句总是与离它最近的if语句配对,所以在if语句的嵌套中一定要注意else语句与哪个if语句匹配 #include <iostream> using namespace std; ...

  9. 树莓派常用Linux命令

    转自小五义 1.ls命令:列出文件目录的常用命令,主要参数见下表. -a 列出目录下的所有文件,包括以.开头的隐含文件. -b 把文件名中不可输出的字符用反斜杠加字符编号(就象在C语言里一样)的形式列 ...

  10. 理解iOS 8中的Self Sizing Cells和Dynamic Type

    http://www.cocoachina.com/ios/20140922/9717.html 在iOS 8中,苹果引入了UITableView的一项新功能--Self Sizing Cells,对 ...