[Jsoi2010]连通数 bitset + Floyd

Code:

#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<bitset>
using namespace std;
void setIO(string a){
	freopen((a+".in").c_str(),"r",stdin);
}


#define maxn 2500
bitset<maxn> G[maxn];
char arr[maxn];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;++i){
		scanf("%s",arr);
		G[i][i]=1;
		for(int j=0;j<n;++j) if(arr[j] == '1') G[i][j]=1;
	}
    for(int k=0;k<n;++k)
    	for(int i=0;i<n;++i)  if(G[k][i]) G[k]|=G[i];
    int ans=0;
    for(int i=0;i<n;++i) ans+=G[i].count();
    printf("%d",ans);
    return 0;

}

  

原文地址:https://www.cnblogs.com/guangheli/p/9911337.html