結果

問題 No.697 池の数はいくつか
ユーザー takeya_okinotakeya_okino
提出日時 2019-08-20 21:23:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 5,677 ms / 6,000 ms
コード長 1,679 bytes
コンパイル時間 1,819 ms
コンパイル使用メモリ 77,432 KB
実行使用メモリ 259,196 KB
最終ジャッジ日時 2024-05-04 06:38:01
合計ジャッジ時間 45,907 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 102 ms
52,764 KB
testcase_01 AC 108 ms
53,480 KB
testcase_02 AC 103 ms
52,948 KB
testcase_03 AC 100 ms
52,948 KB
testcase_04 AC 109 ms
53,708 KB
testcase_05 AC 111 ms
53,792 KB
testcase_06 AC 104 ms
52,772 KB
testcase_07 AC 106 ms
52,748 KB
testcase_08 AC 106 ms
52,628 KB
testcase_09 AC 110 ms
53,860 KB
testcase_10 AC 104 ms
52,496 KB
testcase_11 AC 104 ms
52,940 KB
testcase_12 AC 109 ms
53,800 KB
testcase_13 AC 117 ms
54,108 KB
testcase_14 AC 117 ms
54,232 KB
testcase_15 AC 99 ms
52,776 KB
testcase_16 AC 108 ms
53,416 KB
testcase_17 AC 109 ms
53,820 KB
testcase_18 AC 103 ms
52,600 KB
testcase_19 AC 103 ms
53,824 KB
testcase_20 AC 112 ms
53,932 KB
testcase_21 AC 100 ms
52,912 KB
testcase_22 AC 97 ms
52,796 KB
testcase_23 AC 115 ms
54,172 KB
testcase_24 AC 1,218 ms
89,636 KB
testcase_25 AC 1,251 ms
89,248 KB
testcase_26 AC 1,207 ms
87,264 KB
testcase_27 AC 1,236 ms
89,196 KB
testcase_28 AC 1,255 ms
87,456 KB
testcase_29 AC 5,610 ms
247,600 KB
testcase_30 AC 5,640 ms
256,136 KB
testcase_31 AC 5,677 ms
245,900 KB
testcase_32 AC 5,356 ms
259,196 KB
testcase_33 AC 5,415 ms
255,744 KB
testcase_34 AC 5,579 ms
256,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  static int[] par;
  static int[] rank;
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int h = sc.nextInt();
    int w = sc.nextInt();
    int n = h * w;
    par = new int[n];
    rank = new int[n];
    init(n);

    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) {
          int p = i * w + j;
          if((i > 0) && (a[i - 1][j] == 1)) unite(p, p - w);
          if((i < h - 1) && (a[i + 1][j] == 1)) unite(p, p + w);
          if((j > 0) && (a[i][j - 1] == 1)) unite(p, p - 1);
          if((j < w - 1) && (a[i][j + 1] == 1)) unite(p, p + 1);
        }
      }
    }

    HashSet<Integer> set = new HashSet<Integer>();

    for(int i = 0; i < h; i++) {
      for(int j = 0; j < w; j++) {
        if(a[i][j] == 1) {
          set.add(find(i * w + j));
        }
      }
    }

    System.out.println(set.size());
  }
  static void init(int m) {
      for(int i = 0; i < m; i++) {
        par[i] = i;
        rank[i] = 1;
      }
    }
  static int find(int x) {
      if(par[x] == x) {
        return x;
      } else {
        return par[x] = find(par[x]);
      }
    }
  static void unite(int x, int y) {
      x = find(x);
      y = find(y);
      if(x != y) {
        if(rank[x] < rank[y]) {
          par[x] = y;
        } else {
          par[y] = x;
          if(rank[x] == rank[y]) rank[x]++; 
        }
      }
    }
  static boolean same(int x, int y) {
      return find(x) == find(y);
    }
}
0