結果

問題 No.697 池の数はいくつか
ユーザー IL_mstaIL_msta
提出日時 2018-06-12 19:49:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 928 ms / 6,000 ms
コード長 2,234 bytes
コンパイル時間 1,157 ms
コンパイル使用メモリ 105,804 KB
実行使用メモリ 40,564 KB
最終ジャッジ日時 2023-08-08 02:06:04
合計ジャッジ時間 9,056 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 91 ms
7,388 KB
testcase_25 AC 91 ms
7,412 KB
testcase_26 AC 92 ms
7,384 KB
testcase_27 AC 91 ms
7,464 KB
testcase_28 AC 91 ms
7,352 KB
testcase_29 AC 927 ms
40,404 KB
testcase_30 AC 831 ms
40,564 KB
testcase_31 AC 928 ms
40,404 KB
testcase_32 AC 827 ms
40,484 KB
testcase_33 AC 827 ms
40,492 KB
testcase_34 AC 828 ms
40,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#pragma region include
#include <iostream>
#include <iomanip>
#include <stdio.h>

#include <sstream>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <complex>

#include <string>
#include <cstring>
#include <vector>
#include <bitset>

#include <queue>
#include <set>
#include <map>
#include <stack>
#include <list>

#include <ctime>
////
//#include <random>//
#pragma endregion //#include
/////////

#pragma region typedef
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
#pragma endregion //typedef
////定数
const int INF = (int)1e9;
const LL MOD = (LL)1e9+7;
const LL LINF = (LL)4e18+20;
const LD PI = acos(-1.0);
const double EPS = 1e-9;
/////////
using namespace::std;

class UnionFind2{
public:
	int cNum;//要素数
	vector<int> parent;
	UnionFind2(int n){
		cNum = n;
		parent = vector<int>(n);
		for(int i=0;i<n;++i){
			parent[i] = i;
		}
	}
	int find(int x){
		if( parent[x] == x ){return x;}
		return parent[x] = find( parent[x] );
	}
	bool same(int x,int y){return find(x) == find(y);}
	void add(int x,int y){//union
		x = find(x);
		y = find(y);
		if( x==y )return;
		parent[x] = y;
	}
};
int R,C;
int index(int r,int c){
	return r*C+c;
}
void solve(){
	
	cin>>R>>C;
	vector<vector<bool> > fld(R,vector<bool>(C,false));
	for(int r=0;r<R;++r){
		for(int c=0;c<C;++c){
			int ter;
			cin>>ter;
			if(ter){
				fld[r][c]=true;
			}
		}
	}
	UnionFind2 uf(R*C);
	for(int r=0;r<R;++r){
		for(int c=0;c<C;++c){
			if(fld[r][c]==false){
				continue;
			}
			if(r+1<R){
				if(fld[r+1][c]){
					uf.add(index(r,c),index(r+1,c));
				}
			}
			if(c+1<C){
				if(fld[r][c+1]){
					uf.add(index(r,c),index(r,c+1));
				}
			}
		}
	}
	vector<bool> useRoot(R*C,false);
	for(int r=0;r<R;++r){
		for(int c=0;c<C;++c){
			if(fld[r][c]==true){
				useRoot[uf.find(index(r,c))] = true;
			}
		}
	}
	int ans = 0;
	for(int i=0;i<R*C;++i){
		if(useRoot[i]){
			++ans;
		}
	}

	cout << ans << endl;
}

#pragma region main
signed main(void){
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	std::cout << std::fixed;//小数を10進数表示
	cout << setprecision(16);//小数点以下の桁数を指定//coutとcerrで別	

	solve();
}
#pragma endregion //main()
0