【洛谷P4306】【JSOI2010】—连通数(bitset)

传送门


傻逼题

直接bitsetbitset优化传递闭包
复杂度O(n364)O(\frac{n^3}{64})

好像floydfloyd写错的都可以过。。。

#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
	static char ibuf[RLEN],*ib,*ob;
	(ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
	char ch=gc();
	int res=0,f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
	return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define cs const
#define poly vector<int>
cs int mod=1e9+7;
inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
inline void Add(int &a,int b){a=add(a,b);}
inline int dec(int a,int b){return a>=b?a-b:a-b+mod;}
inline void Dec(int &a,int b){a=dec(a,b);}
inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,a=mul(a,a))(b&1)?(res=mul(res,a)):0;return res;}
inline void chemx(int &a,int b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
cs int N=2002;
bitset<N> c[N];
char s[N];
int n;
int main(){
	n=read();
	for(int i=1;i<=n;i++){
		scanf("%s",s+1);
		for(int j=1;j<=n;j++)if(i^j)c[i][j]=(s[j]=='1');else c[i][j]=1;
	}
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)if(c[j][i])c[j]|=c[i];
	int res=0;
	for(int i=1;i<=n;i++)res+=c[i].count();
	cout<<res;
}
posted @ 2019-08-21 21:45  Stargazer_cykoi  阅读(124)  评论(0编辑  收藏  举报