【BZOJ2208】【JSOI2010】连通数 传递闭包

该博客介绍了如何解决一个关于连通数的问题,利用传递闭包的方法,通过Floyd算法处理有向图中两点之间的可达性。博主提出了使用bitset进行优化,从而提高效率,时间复杂度达到O(n^3/64)。文章包括题目描述、题解思路和实现代码。
摘要由CSDN通过智能技术生成

题目描述

  定义一个图的连通度为图中可达顶点对的数目。给你一个 n 个点的有向图,问你这个图的连通度。

  n2000,mn2

题解

  一个很简单的做法就是传递闭包:像floyd算法一样处理两个点之间是否可达。

fi,j|=fi,k&fk,j

  但是这是 O(n3) 的。

  观察到用到的运算都是位运算,那就用bitset加速一下就行了。

  时间复杂度: O(n364) (还是 O(n3)

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<cmath>
#include<functional>
#include<bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void sort(int &a,int &b)
{
    if(a>b)
        swap(a,b);
}
void open(const char *s)
{
#ifndef ONLINE_JUDGE
    char str[100];
    sprintf(str,"%s.in",s);
    freopen(str,"r",stdin);
    sprintf(str,"%s.out",s);
    freopen(str,"w",stdout);
#endif
}
int rd()
{
    int s=0,c;
    while((c=getchar())<'0'||c>'9');
    do
    {
        s=s*10+c-'0';
    }
    while((c=getchar())>='0'&&c<='9');
    return s;
}
int upmin(int &a,int b)
{
    if(b<a)
    {
        a=b;
        return 1;
    }
    return 0;
}
int upmax(int &a,int b)
{
    if(b>a)
    {
        a=b;
        return 1;
    }
    return 0;
}
bitset<2001> f[2010];
char s[2010];
int main()
{
    int n;
    int i,j;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%s",s+1);
        for(j=1;j<=n;j++)
            if(s[j]-'0')
                f[i].set(j);
        f[i].set(i);
    }
    for(j=1;j<=n;j++)
        for(i=1;i<=n;i++)
            if(i!=j&&f[i][j])
                f[i]|=f[j];
    int s=0;
    for(i=1;i<=n;i++)
        s+=f[i].count();
    printf("%d\n",s);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值