zoukankan      html  css  js  c++  java
  • 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
    ",ans);
        return 0;
    }
    

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

  • 相关阅读:
    du 命令
    iostat 命令
    sar 命令
    mkdir 命令
    time 命令
    date 命令
    history 命令
    vmstat 命令
    pmap 命令
    df 命令
  • 原文地址:https://www.cnblogs.com/fengxunling/p/10565225.html
Copyright © 2011-2022 走看看