JSOI2010 连通数

题目链接:戳我

用bitset优化一下floyd。。。。
如果to[i][j]true,那么to[j][k]1也可以转化成to[i][k]==1。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<bitset>
using namespace std;
int n,ans;
char s[2010];
bitset<2010>to[2010];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("ce.in","r",stdin);
    #endif
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",s+1);
        for(int j=1,len=strlen(s+1);j<=len;j++)
            if(s[j]=='1')
                to[i][j]=1;
        to[i][i]=1;
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(to[i][j]) to[i]|=to[j];
    for(int i=1;i<=n;i++) ans+=to[i].count();
    printf("%d\n",ans);
    return 0;
}

update:bitset的位运算是O(n)/64的,就是在这一步压位进行了优化qwq

posted @ 2019-03-20 15:31  风浔凌  阅读(138)  评论(0编辑  收藏  举报