BZOJ 2208: [Jsoi2010]连通数【floyd】【bitset】

b i t s e t bitset 卡常很优秀吖

#include <cmath>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define db double
#define sg string
#define ll long long
#define rel(i,x,y) for(ll i=(x);i<(y);i++)
#define rep(i,x,y) for(ll i=(x);i<=(y);i++)
#define red(i,x,y) for(ll i=(x);i>=(y);i--)
#define res(i,x) for(ll i=head[x];i;i=nxt[i])
using namespace std;

const ll N=2e3+5;
const ll Inf=1e18;
const db Eps=1e-10;

char ch[N];ll n,ans;

bitset<N>f[N];

inline ll read() {
	ll x=0;char ch=getchar();bool f=0;
	while(ch>'9'||ch<'0'){if(ch=='-')f=1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return f?-x:x;
}

int main() {
	n=read();
	
	rep(i,1,n) {
		scanf("%s",ch+1);
		rep(j,1,n) if(ch[j]=='1'||i==j) f[i][j]=1;
	}

	rep(i,1,n) rep(j,1,n) if(f[j].test(i)) f[j]|=f[i];
		
	rep(i,1,n) ans+=f[i].count();

	printf("%lld\n",ans);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/yanzhenhuai/article/details/83417313