题目链接

题意

有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. PS中如何把图片颜色加到字体上去

    1.在PS中的图层中,将图片置于文字层的上方,同时按ctrl+alt+g键,这样就将文字范围以外的图像给剪切掉了.见附图下方的效果. 2.最终效果如下图: 参见:https://zhidao.baid ...

  2. java学习之—排序

    package test3; public class Sort{ /** * 冒泡排序 * @param array */ public void bubbleSort(int[] array) { ...

  3. DAY08、文件操作

    一.文件操作模式汇总: 主模式: r:读模式 w:写模式(无创建,有清空) a:追加(有创建的功能) x:写,必须自己创建文件,否则报错 从模式: t:文本操作(默认模式)r >rt,w> ...

  4. linux服务器运维

    1. grep正则匹配 grep -E  "([0-9]{1,3}\.){4}" filepath egrep "([0-9]{1,3}\.){4}"  fil ...

  5. vue-cli: preset预设

    preset:预设 vue create demo01 过程中,会保存预设,自动保存着 .vuerc 文件中 .vuerc 文件的位置:C:\Users\Administrator C:\Users\ ...

  6. 安装zip版mysql

    MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大家的英文可以看懂英文提示),一般MySQL将会安装在C:\P ...

  7. BZOJ2084[Poi2010]Antisymmetry——回文自动机

    题目描述 对于一个01字符串,如果将这个字符串0和1取反后,再将整个串反过来和原串一样,就称作“反对称”字符串.比如00001111和010101就是反对称的,1001就不是.现在给出一个长度为N的0 ...

  8. CodeForces615A-Bulbs-模拟

    水题 #include <cstdio> #include <algorithm> using namespace std; ]; int main() { scanf(&qu ...

  9. HDU - 3917(朴素LIS + 最大流)

    题意: 求出所有的最长上升子序列的个数且每个元素只能用一次 解析: 呵...呵...呵..呵..emm... 再见 我死了...wa了15发之后...原来不能用~  要用 != EOF 这题算水题吧. ...

  10. 【BZOJ1004】【HNOI2008】Cards 群论 置换 burnside引理 背包DP

    题目描述 有\(n\)张卡牌,要求你给这些卡牌染上RGB三种颜色,\(r\)张红色,\(g\)张绿色,\(b\)张蓝色. 还有\(m\)种洗牌方法,每种洗牌方法是一种置换.保证任意多次洗牌都可用这\( ...