結果

問題 No.697 池の数はいくつか
ユーザー Daigo HIROOKADaigo HIROOKA
提出日時 2018-06-23 03:38:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 5,964 ms / 6,000 ms
コード長 1,517 bytes
コンパイル時間 3,791 ms
コンパイル使用メモリ 77,768 KB
実行使用メモリ 176,944 KB
最終ジャッジ日時 2023-12-26 11:38:42
合計ジャッジ時間 52,302 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
57,976 KB
testcase_01 AC 136 ms
57,968 KB
testcase_02 AC 137 ms
57,976 KB
testcase_03 AC 134 ms
57,960 KB
testcase_04 AC 125 ms
56,692 KB
testcase_05 AC 134 ms
57,852 KB
testcase_06 AC 138 ms
57,972 KB
testcase_07 AC 134 ms
57,980 KB
testcase_08 AC 134 ms
57,940 KB
testcase_09 AC 135 ms
57,844 KB
testcase_10 AC 141 ms
57,864 KB
testcase_11 AC 135 ms
57,960 KB
testcase_12 AC 133 ms
57,964 KB
testcase_13 AC 141 ms
58,092 KB
testcase_14 AC 137 ms
57,960 KB
testcase_15 AC 134 ms
57,852 KB
testcase_16 AC 134 ms
57,980 KB
testcase_17 AC 135 ms
57,840 KB
testcase_18 AC 137 ms
57,940 KB
testcase_19 AC 133 ms
57,960 KB
testcase_20 AC 138 ms
57,840 KB
testcase_21 AC 137 ms
57,956 KB
testcase_22 AC 133 ms
57,956 KB
testcase_23 AC 144 ms
57,944 KB
testcase_24 AC 1,313 ms
80,016 KB
testcase_25 AC 1,288 ms
79,972 KB
testcase_26 AC 1,375 ms
80,052 KB
testcase_27 AC 1,314 ms
80,028 KB
testcase_28 AC 1,320 ms
79,988 KB
testcase_29 AC 5,845 ms
176,748 KB
testcase_30 AC 5,964 ms
174,644 KB
testcase_31 AC 5,741 ms
176,776 KB
testcase_32 AC 5,888 ms
176,700 KB
testcase_33 AC 5,646 ms
176,944 KB
testcase_34 AC 5,695 ms
176,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class No697{
	public static void main(String[] args){
		run();
	}

	static class DJSet{
		int n;
		int[] upper;

		public DJSet(int n_) {
			n = n_;
			upper = new int[n];
			Arrays.fill(upper, -1);
		}

		int root(int x) {
			return upper[x] < 0 ? x : (upper[x] = root(upper[x]));
		}

		boolean equiv(int x, int y) {
			return root(x) == root(y);
		}

		void setUnion(int x, int y) {
			x = root(x);
			y = root(y);
			if (x == y)
				return;
			if (upper[x] > upper[y]) {
				x ^= y;
				y ^= x;
				x ^= y;
			}
			upper[x] += upper[y];
			upper[y] = x;
		}
	}

	static void run(){
		Scanner sc = new Scanner(System.in);
		int H = sc.nextInt();
		int W = sc.nextInt();
		DJSet ds = new DJSet(H * W);
		int[][] A = new int[H][W];
		for (int i = 0; i < H; ++i) {
			for (int j = 0; j < W; ++j) {
				A[i][j] = sc.nextInt();
			}
		}
		for (int i = 0; i < H; ++i) {
			for (int j = 0; j < W; ++j) {
				if (A[i][j] != 1)
					continue;
				for (int di = -1; di <= 1; ++di) {
					for (int dj = -1; dj <= 1; ++dj) {
						if (Math.abs(di) + Math.abs(dj) != 1)
							continue;
						int ni = i + di;
						int nj = j + dj;
						if (ni < 0 || nj < 0 || ni >= H || nj >= W)
							continue;
						if (A[ni][nj] != 1)
							continue;
						ds.setUnion(i * W + j, ni * W + nj);
					}
				}
			}
		}
		int cnt = 0;
		for (int i = 0; i < H; ++i) {
			for (int j = 0; j < W; ++j) {
				if (A[i][j] == 1 && i * W + j == ds.root(i * W + j))
					++cnt;
			}
		}
		System.out.println(cnt);
	}
}

0