結果

問題 No.697 池の数はいくつか
ユーザー furafura
提出日時 2020-05-12 06:25:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 913 ms / 6,000 ms
コード長 855 bytes
コンパイル時間 2,076 ms
コンパイル使用メモリ 196,924 KB
実行使用メモリ 73,780 KB
最終ジャッジ日時 2024-04-25 20:46:17
合計ジャッジ時間 10,088 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 1 ms
6,944 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 106 ms
20,088 KB
testcase_25 AC 103 ms
18,816 KB
testcase_26 AC 104 ms
20,144 KB
testcase_27 AC 106 ms
18,680 KB
testcase_28 AC 105 ms
20,000 KB
testcase_29 AC 848 ms
73,632 KB
testcase_30 AC 901 ms
73,708 KB
testcase_31 AC 847 ms
73,780 KB
testcase_32 AC 908 ms
73,556 KB
testcase_33 AC 913 ms
73,684 KB
testcase_34 AC 909 ms
73,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

class union_find{
	int n;
	vector<int> p;
public:
	union_find(int n):n(n),p(n,-1){}
	int find(int u){ return p[u]<0?u:p[u]=find(p[u]); }
	void unite(int u,int v){
		u=find(u); v=find(v);
		if(u!=v){
			if(p[v]<p[u]) swap(u,v);
			p[u]+=p[v]; p[v]=u; n--;
		}
	}
	bool is_same(int u,int v){ return find(u)==find(v); }
	int size()const{ return n; }
	int size(int u){ return -p[find(u)]; }
};

int main(){
	int h,w; scanf("%d%d",&h,&w);
	static int B[3000][3000];
	rep(i,h) rep(j,w) scanf("%d",&B[i][j]);

	union_find U(h*w);
	rep(i,h) rep(j,w) if(B[i][j]==1) {
		if(j<w-1 && B[i][j+1]==1) U.unite(i*w+j,i*w+(j+1));
		if(i<h-1 && B[i+1][j]==1) U.unite(i*w+j,(i+1)*w+j);
	}

	int ans=U.size();
	rep(i,h) rep(j,w) if(B[i][j]==0) ans--;
	printf("%d\n",ans);

	return 0;
}
0