题目链接

题意

有n个房间,每个房间里面有若干把钥匙,每把钥匙可以打开对应的一扇门。如果手中没有钥匙,就要随机轰炸一个房间来打开这个房间。如果有钥匙,就要去打开这些房间。问期望轰炸次数是多少。

思路

根据期望的线性性质,总期望轰炸次数就是每个房间被轰炸的概率\(\times\) 1。

所以就考虑每个房间被轰炸的概率。

先理解一下题意,也就是说只要打开了一扇门就可以打开若干扇门。所以可以从每个房间向他房间中钥匙所对应的房间连边。

每个房间被轰炸当且仅当所有可以到达他的房间都没有被轰炸。

所以把上面所说的边放过了。跑一边传递闭包。第i个点被轰炸的概率就是\(1/anc(i)\),\(anc(x)\)表示在传递闭包中x所能到达的点的个数。

求传递闭包可以用\(floyd\),再加上\(bitset\)的优化

所以最终答案就是$$\sum_{i=1}^n\frac{1}{anc_i}$$

代码

/*
* @Author: wxyww
* @Date: 2019-01-23 15:39:31
* @Last Modified time: 2019-01-23 16:03:40
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<bitset>
using namespace std;
typedef long long ll;
const int N = 1010;
bitset<N>f[N];
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
} void solve(int x) {
int n = read();
for(int i = 1;i <= n;++i) {
int k = read();
f[i].set(i);
for(int j = 1;j <= k;++j) {
int x = read();
f[x].set(i);
}
}
for(int k = 1;k <= n;++k)
for(int i = 1;i <= n;++i)
if(f[i][k]) f[i] |= f[k];
double ans = 0;
for(int i = 1;i <= n;++i) {
ans += (double)1 / f[i].count();
f[i].reset();
}
printf("Case #%d: %.5lf\n",x,ans);
}
int main() {
int T = read();
for(int i = 1;i <= T;++i) {
// MEM();
solve(i);
}
return 0;
}

hdu5306 Explosion的更多相关文章

  1. NBUT 1635 Explosion(最小顶点覆盖)

    [1635] Explosion 时间限制: 10000 ms 内存限制: 65535 K 问题描述 there is a country which contains n cities connec ...

  2. Codeforces Gym 191033 E. Explosion Exploit (记忆化搜索+状压)

    E. Explosion Exploit time limit per test 2.0 s memory limit per test 256 MB input standard input out ...

  3. hdu5306 Gorgeous Sequence

    hdu5306 Gorgeous Sequence 题目大意 ​ 给你一个序列,维护区间和,区间chkmin和区间最大值 数据范围 数据组数T,序列长度n,操作次数m $T = 100,\sum n ...

  4. 【hdu5306】 Gorgeous Sequence

    http://acm.hdu.edu.cn/showproblem.php?pid=5306 (题目链接) 题意 区间取$min$操作,区间求和操作,区间求最值操作. Solution 乱搞一通竟然A ...

  5. Explosion at Cafebazaar

    Explosion at Cafebazaar 时间限制: 1 Sec  内存限制: 128 MB 题目描述 You are an engineer at Cafebazaar and your sp ...

  6. 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)-E. Explosion Exploit-概率+状压dp

    2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)-E. Explosion Exploit-概率+状压dp [P ...

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

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

  8. HDU - 5036 Explosion

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

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

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

随机推荐

  1. C# Note15:设置Window图标的正确方式

    Windows Presentation Foundation(WPF)独立应用程序有两种类型的图标: 一个程序集(assembly) 图标,通过在应用程序的项目构建文件中使用<Applicat ...

  2. liunx 运维知识二部分

    Windows下的目录和Linux系统下的目录有什么区别? Windows目录下的文件一般都是分区(C盘,D盘...),C盘下面有什么目录,目录下面还有其他目录加上文件. Linux系统目录结构一切都 ...

  3. 浅谈基于Prism的软件系统的架构设计

    很早就想写这么一篇文章来对近几年使用Prism框架来设计软件来做一次深入的分析了,但直到最近才开始整理,说到软件系统的设计这里面有太多的学问,只有经过大量的探索才能够设计出好的软件产品,就本人的理解, ...

  4. zabbix添加监控Mysql

    起因:zabbix自带的mysql监控模板直接使用会显示“不支持的”因为key的值是通过Mysql用户查看"show global status"信息或者用mysqladmin命令 ...

  5. ServiceLoader详解

    系统中用到了ServiceLoader,查了一下: ServiceLoader与ClassLoader是Java中2个即相互区别又相互联系的加载器.JVM利用ClassLoader将类载入内存,这是一 ...

  6. Flask从入门到精通

    1. flask的下载和安装 在windows下安装flask flask依赖于两个库, 即werkzeug 和jinja2 , werkzeug负责服务器的部署, jinja2负责模板引擎,管理安装 ...

  7. vs code軟件操作

    https://www.imooc.com/article/39349 https://www.html.cn/archives/8144

  8. vue之v-for使用说明

    demo.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/19 ...

  9. 2.docker的网络模式

    本篇文章使用nginx:apline  镜像进行编辑. docker 版本基于 [root@master song]# docker version Client: Version: API vers ...

  10. Visual Studio 2017 and Swagger: Building and Documenting Web APIs

    Swagger是一种与技术无关的标准,允许发现REST API,为任何软件提供了一种识别REST API功能的方法. 这比看起来更重要:这是一个改变游戏技术的方式,就像Web服务描述语言一样WSDL( ...