2014-09-26 00:38:37
思路:bitset启蒙题,考虑每个门,计算暴力打开哪些门(k个)就可以顺便打开这扇门,于是必须爆开这k个门中的一个,所以爆开这扇门的期望是1/k,答案就是所有门期望的和。
1 /************************************************************************* 2 3 > File Name: 5036.cpp 4 > Author: Nature 5 > Mail: 564374850@qq.com 6 > Created Time: Thu 25 Sep 2014 11:43:25 PM CST 7 ************************************************************************/ 8 9 #include <cstdio> 10 #include <cstring> 11 #include <cstdlib> 12 #include <cmath> 13 #include <vector> 14 #include <queue> 15 #include <bitset> 16 #include <iostream> 17 #include <algorithm> 18 using namespace std; 19 typedef long long ll; 20 const int INF = 1 << 29; 21 const int maxn = 1010; 22 23 int T,N; 24 bitset<maxn> bset[maxn]; 25 26 int main(){ 27 int k,v; 28 scanf("%d",&T); 29 for(int t = 1; t <= T; ++t){ 30 scanf("%d",&N); 31 for(int i = 0; i < N; ++i){ 32 bset[i].reset(); 33 bset[i][i] = true; 34 scanf("%d",&k); 35 while(k--){ 36 scanf("%d",&v); 37 bset[i][v - 1] = true; 38 } 39 } 40 for(int j = 0; j < N; ++j){ 41 for(int i = 0; i < N; ++i) 42 if(bset[i][j]) 43 bset[i] |= bset[j]; 44 } 45 double ans = 0.0; 46 for(int i = 0; i < N; ++i){ 47 int cnt = 0; 48 for(int j = 0; j < N; ++j){ 49 if(bset[j][i]) ++cnt; 50 } 51 ans += 1.0 / cnt; 52 } 53 printf("Case #%d: %.5lf\n",t,ans); 54 } 55 return 0; 56 }