和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;
}