結果

問題 No.697 池の数はいくつか
ユーザー takeya_okinotakeya_okino
提出日時 2019-07-24 19:56:50
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,827 bytes
コンパイル時間 1,940 ms
コンパイル使用メモリ 77,752 KB
実行使用メモリ 259,936 KB
最終ジャッジ日時 2024-05-04 06:36:01
合計ジャッジ時間 38,079 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 111 ms
41,484 KB
testcase_01 AC 116 ms
41,904 KB
testcase_02 AC 112 ms
42,152 KB
testcase_03 AC 112 ms
41,280 KB
testcase_04 AC 121 ms
41,528 KB
testcase_05 AC 104 ms
41,420 KB
testcase_06 AC 110 ms
41,620 KB
testcase_07 AC 111 ms
41,604 KB
testcase_08 AC 112 ms
41,440 KB
testcase_09 AC 111 ms
41,828 KB
testcase_10 AC 113 ms
41,740 KB
testcase_11 AC 103 ms
41,504 KB
testcase_12 AC 116 ms
41,428 KB
testcase_13 AC 119 ms
42,132 KB
testcase_14 AC 121 ms
41,876 KB
testcase_15 AC 115 ms
41,400 KB
testcase_16 AC 114 ms
41,960 KB
testcase_17 AC 117 ms
41,616 KB
testcase_18 AC 109 ms
41,944 KB
testcase_19 AC 116 ms
41,340 KB
testcase_20 AC 121 ms
41,488 KB
testcase_21 AC 106 ms
41,436 KB
testcase_22 AC 107 ms
41,540 KB
testcase_23 AC 126 ms
41,908 KB
testcase_24 AC 1,221 ms
76,496 KB
testcase_25 AC 1,271 ms
78,464 KB
testcase_26 AC 1,257 ms
76,412 KB
testcase_27 AC 1,285 ms
78,744 KB
testcase_28 AC 1,263 ms
78,576 KB
testcase_29 AC 5,556 ms
243,620 KB
testcase_30 TLE -
testcase_31 TLE -
testcase_32 AC 5,755 ms
256,588 KB
testcase_33 AC 5,601 ms
259,936 KB
testcase_34 AC 5,662 ms
257,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  static int[] par;
  static int[] rank;
  static int[][] a;
  static int h;
  static int w;
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    h = sc.nextInt();
    w = sc.nextInt();
    par = new int[h * w];
    rank = new int[h * w];
    a = new int[h][w];
    init(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) {
          if((i - 1) >= 0) unite((i * w + j), ((i - 1) * w + j));
          if((i + 1) < h) unite((i * w + j), ((i + 1) * w + j));
          if((j - 1) >= 0) unite((i * w + j), (i * w + j - 1));
          if((j + 1) < w) unite((i * w + j), (i * w + j + 1));
        }
      }
    }
    HashSet<Integer> set = new HashSet<Integer>();
    for(int i = 0; i < (h * w); i++) {
      int i1 = i / w;
      int j1 = i - w * i1;
      if(a[i1][j1] == 1) set.add(find(i));
    }
    System.out.println(set.size());
  }
  static void init(int m) {
      for(int i = 0; i < m; i++) {
        par[i] = i;
        rank[i] = 0;
      }
    }
  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) {
      int i1 = x / w;
      int j1 = x - i1 * w;
      int i2 = y / w;
      int j2 = y - i2 * w;
      if(a[i1][j1] == a[i2][j2]) {
      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