計蒜課 引爆炸彈 並查集

其中用set代替unique可以AC原因未知

解法

本質是在求聯通塊,用並查集,用暴力。

ACcode

#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<stack>
#include<ctime>
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val)	 memset(var,val,sizeof(var))
#define LL long long 
#define eps 0.000001
#define inf 0x7f7f7f7f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
	while ( a>='0' && a<='9' ){	x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define N 1003
int ma[N][N];int fa[N],num[N][N],tot = 0 ;  
int findfa(int x){
	return x == fa[x] ? x : fa[x] = findfa(fa[x]) ; 
}
void unin(int x,int y){
	int xfa = findfa(x); int yfa = findfa(y);
	if ( xfa != yfa ){
		fa[xfa] = yfa; 
	}
}
int main()
{
//	freopen("1.txt","r",stdin);
	int n = read(), m = read();
	mset(num,0);	
	rep(i,1,n){
		rep(j,1,m){
			scanf("%1d",&ma[i][j]);
			if( ma[i][j] == 1 )
				num[ i ][ j ] = ++tot; 
		}	
	}	
	rep(i,1,tot)	fa[i] = i; 
	rep(i,1,n){
		bool is = 0 ; int st; 
		rep(j,1,m){
			if ( num[i][j] != 0 ){
				if ( is == 0 ){
					is = 1; 
					st = num[i][j];
				}
				else {
					unin(st,num[i][j]);
				}
			}
		}
		is = 0 ; 
	}
	rep(j,1,m){
		bool is = 0 ; int st; 
		rep(i,1,n){
			if ( num[i][j] != 0 ){
				if ( is == 0 ){
					is = 1; 
					st = num[i][j];
				}
				else {
					unin(st,num[i][j]);
				}
			}
		}
		is = 0; 
	}	
	set<int>s; 
	rep(i,1,tot){
		findfa(i);	
		s.insert(fa[i]);
	}
	cout<<s.size()<<endl; 
	return 0;
}

另外一種解法

#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<stack>
#include<ctime>
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val)	 memset(var,val,sizeof(var))
#define LL long long 
#define eps 0.000001
#define inf 0x7f7f7f7f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
	while ( a>='0' && a<='9' ){	x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define N 1003
int n,m; int ma[N][N];int row[N],col[N];
void boom(int x,int y){
	ma[x][y] = 0; 
	if ( row[x] == 0 ){
		row[x] = 1; 
		for (int i = 1; i <= m; i++){
			if ( ma[x][i] == 1 ){
				boom(x,i);
			}
		}
	}
	if ( col[y] == 0 ){
		col[y] = 1; 
		for (int i = 1; i <= n; i++){
			if ( ma[i][y] == 1 ){
				boom(i,y);
			}
		}
	}
}
int main()
{
//	freopen("1.txt","r",stdin);
	n = read(), m = read();
	rep(i,1,n){
		rep(j,1,m){
			scanf("%1d",&ma[i][j]);
		}	
	}	
	mset(row,0) ; 
	mset(col,0);
	int sum = 0 ; 
	rep(i,1,n){
		rep(j,1,m){
			if( ma[i][j] == 1 ){
				boom(i,j);				
				sum++;
			}
		}	
	}	
	cout<<sum<<endl; 
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章