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  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<=) indicating the number of rooms. 

The following N lines corresponde to the rooms from  to N. Each line begins with an integer k (<=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 ), y is the answer which should be rounded to  decimal places.
 
Sample Input

 
Sample Output
Case #: 1.00000
Case #: 3.00000
 
Source
 
题意:
一个人要打开或者用炸弹砸开所有的门,每个门里面有一些钥匙,一个钥匙对应一个门,有了一个门的钥匙就能打开相应的门,告诉每个门里面有哪些门的钥匙,问需要用的炸弹为多少。
 
思路:
考虑每个点需要用炸弹打开的概率,那么所有点的概率之和就是解。 每个点 v 需要用炸弹打开的概率是 1/S, S是u(u->v联通)的数量, 然后就变成求这个图的传递闭包了,用bitset优化。为什么是1/S呢,因为这S个门里面必定要用一个炸弹砸开,而砸v的概率为1/S。
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stdlib.h>
#include<bitset>
using namespace std;
#define N 1006
int n;
bitset<N> bs[N];
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<N;i++){
bs[i].reset();
bs[i][i]=true;
}
for(int i=;i<n;i++){
int k;
scanf("%d",&k);
for(int j=;j<k;j++){
int x;
scanf("%d",&x);
x--;
bs[i][x]=true;
}
}
for(int j=;j<n;j++){
for(int i=;i<n;i++){
if(bs[i][j]){
bs[i] |= bs[j];
}
}
}
double ans=;
for(int j=;j<n;j++){
int cnt=;
for(int i=;i<n;i++){
if(bs[i][j]){
cnt++;
}
}
ans+=1.0/cnt;
}
printf("Case #%d: %.5lf\n",++ac,ans);
}
return ;
}
Recommend

hdu 5036 Explosion(概率期望+bitset)的更多相关文章

  1. hdu 5036 Explosion bitset优化floyd

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

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

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

  3. HDU - 5036 Explosion

    Problem Description Everyone knows Matt enjoys playing games very much. Now, he is playing such a ga ...

  4. 【BZOJ-1419】Red is good 概率期望DP

    1419: Red is good Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 660  Solved: 257[Submit][Status][Di ...

  5. uvalive 7331 Hovering Hornet 半平面交+概率期望

    题意:一个骰子在一个人正方形内,蜜蜂在任意一个位置可以出现,问看到点数的期望. 思路:半平面交+概率期望 #include<cstdio> #include<cstring> ...

  6. OI队内测试一【数论概率期望】

    版权声明:未经本人允许,擅自转载,一旦发现将严肃处理,情节严重者,将追究法律责任! 序:代码部分待更[因为在家写博客,代码保存在机房] 测试分数:110 本应分数:160 改完分数:200 T1: 题 ...

  7. 2016 多校联赛7 Balls and Boxes(概率期望)

    Mr. Chopsticks is interested in random phenomena, and he conducts an experiment to study randomness. ...

  8. 牛客网多校赛第9场 E-Music Game【概率期望】【逆元】

    链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

  9. 【bzoj4832】[Lydsy2017年4月月赛]抵制克苏恩 概率期望dp

    题目描述 你分别有a.b.c个血量为1.2.3的奴隶主,假设英雄血量无限,问:如果对面下出一个K点攻击力的克苏恩,你的英雄期望会受到到多少伤害. 输入 输入包含多局游戏. 第一行包含一个整数 T (T ...

随机推荐

  1. vbox下centos安装增加功能失败

    一般都是:unable to find the sources of your current Linux kernel. 先尝试这个吧:yum install kernel kernel-heade ...

  2. python标准库基础之mmap:内存映射文件

    #作用:建立内存映射文件而不是直接读取内容文本信息内容:如下(名称是text.txt) Lorem ipsum dolor sit amet, consectetuer adipiscing elit ...

  3. 编写jeb插件打印目标方法的交叉引用

    jeb插件的编写,文档在文件夹\jeb_1.5\doc\apidoc. 我这里的目的是回溯sendTextMessage等敏感api的调用路径,实现代码如下: from jeb.api import ...

  4. POJ 3450 Corporate Identity (KMP+暴搞)

    题意: 给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个. 找其中一个字符串,枚举它的所有的字串,然后,逐个kmp比较.......相当暴力,可二分优化. #include & ...

  5. Swift自适应布局(Adaptive Layout)教程(二)

    给TextContainer中添加内容 打开 Main.storyboard ,从组件库(Object Library)中拖拽两个 Label 组件到TextContainer中,位置可以随意摆放: ...

  6. 深入浅出理解iOS经常使用的正則表達式—基础篇[Foundation]

    參考资料:cocoachina的zys475481075的文章 几个单词 Regular ['regjʊlə]adj. 定期的:有规律的 Expression[ɪk'spreʃ(ə)n; ek-] n ...

  7. SQL函数简述

    数字函数ABS 取绝对值 POWER 乘方 LN 10为底数取幂SQRT 平方根 EXP e的n次乘方 LOG(m,n) m为底数n取幂数学运算函数:ACOS ATAN ATAN2 COS COSH ...

  8. docker 1.12.3版本搭建私有仓库,上传镜像报错:server gave HTTP response to HTTPS client”

    系统环境:centos7 docker版本: 1.12.3(注意版本,可能存在不同版本设置不同的情况) docker registry版本:2.4.1 问题: 成功安装docker registry, ...

  9. Jquery:Jquery中的事件<二>

    这几天快忙死了,办了离职还得办入职,完全打乱了我的计划,但是能有一个理想的工作,还是很开心的,以后加把劲,争取把计划再赶上来!不说了,学习!!! 五.事件对象的属性 1.event.type:获取事件 ...

  10. iOS的Bundle资源束制作

    在静态库的制作中,很多时候我们的静态库也是带着文件,图片和多媒体资源的. 若只是直接加入到项目中也是可以,但是,考虑到方便管理(方便插件使用者的管理),我们希望把插件的资源文件打成一个包来管理. 当然 ...