hdu5032 关于bitset传递闭包

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<bitset>
 4 using namespace std;
 5 bitset<1005>edge[1005];
 6 int main()
 7 {
 8     int i,sum,T,t,j,n,x,y;
 9     double ans;
10     scanf("%d",&T);
11     for (t=1;t<=T;t++)
12     {
13         scanf("%d",&n);
14         for (i=1;i<=n;i++)
15         {
16             edge[i].reset();
17             edge[i][i]=1;
18         }
19         for (i=1;i<=n;i++)
20         {
21             scanf("%d",&x);
22             while (x--)
23             {
24                 scanf("%d",&y);
25                 edge[i][y]=1;
26             }
27         }
28         for (i=1;i<=n;i++)
29             for (j=1;j<=n;j++)
30             if (edge[j][i]==1) edge[j]|=edge[i];
31         ans=0.0;
32         for (i=1;i<=n;i++)
33         {
34             sum=0;
35             for (j=1;j<=n;j++)
36                 if (edge[j][i]==1) sum++;
37             ans+=1.0/sum;
38         }
39         printf("Case #%d: %0.5lf\n",t,ans);
40     }
41 }

http://acm.hdu.edu.cn/showproblem.php?pid=5036

posted on 2014-09-25 21:38  xiao_xin  阅读(195)  评论(0编辑  收藏  举报

导航