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

其实我还是不知道bitset到底优化在哪里了。。。知道了再补上qwqwq

猜你喜欢

转载自www.cnblogs.com/fengxunling/p/10565225.html