欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

HDU5036 Explosion(期望 bitset)

程序员文章站 2022-04-09 18:54:39
题意 "题目链接" Sol 和cf上的一道题几乎一摸一样 首先根据期望的线性性,可以转化为求每个点的期望打开次数,又因为每个点最多会被打开一次,只要算每个点被打开的概率就行了 设$anc[i]$表示$i$的反图中能到达的点集大小,答案等于$\sum_{i = 1}^n \frac{1}{anc[i] ......

题意

题目链接

sol

和cf上的一道题几乎一摸一样

首先根据期望的线性性,可以转化为求每个点的期望打开次数,又因为每个点最多会被打开一次,只要算每个点被打开的概率就行了

\(anc[i]\)表示\(i\)的反图中能到达的点集大小,答案等于\(\sum_{i = 1}^n \frac{1}{anc[i]}\)(也就是要保证是第一个被选的)

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1001;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    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;
}
int n;
bitset<maxn> f[maxn], empty;
double solve() {
    n = read();
    for(int i = 1; i <= n; i++) f[i] = empty;
    for(int i = 1; i <= n; i++) {
        int k = read();  f[i].set(i);
        for(int j = 1; j <= k; j++) {
            int v = read(); f[v].set(i);
        }
    }
    for(int k = 1; k <= n; k++) 
        for(int i = 1; i <= n; i++) 
            if(f[i][k]) f[i] = f[i] | f[k];
    double ans = 0;
    for(int i = 1; i <= n; i++) ans += 1.0 / f[i].count();
    return ans;
}
int main() {
    int t = read();
    for(int i = 1; i <= t; i++) printf("case #%d: %.5lf\n", i, solve());
    return 0;
}