結果

問題 No.697 池の数はいくつか
ユーザー takeya_okinotakeya_okino
提出日時 2019-07-24 20:03:22
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,682 bytes
コンパイル時間 2,977 ms
コンパイル使用メモリ 78,360 KB
実行使用メモリ 219,904 KB
最終ジャッジ日時 2024-05-04 06:34:17
合計ジャッジ時間 48,924 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 107 ms
39,944 KB
testcase_01 AC 102 ms
39,948 KB
testcase_02 AC 108 ms
40,448 KB
testcase_03 AC 116 ms
41,104 KB
testcase_04 AC 101 ms
39,804 KB
testcase_05 AC 112 ms
41,664 KB
testcase_06 AC 106 ms
40,588 KB
testcase_07 AC 113 ms
41,236 KB
testcase_08 AC 114 ms
41,280 KB
testcase_09 AC 109 ms
41,172 KB
testcase_10 AC 117 ms
41,364 KB
testcase_11 AC 121 ms
41,592 KB
testcase_12 AC 102 ms
40,216 KB
testcase_13 AC 123 ms
41,752 KB
testcase_14 AC 123 ms
41,472 KB
testcase_15 AC 123 ms
41,648 KB
testcase_16 AC 118 ms
41,068 KB
testcase_17 AC 113 ms
41,324 KB
testcase_18 AC 110 ms
40,620 KB
testcase_19 AC 103 ms
40,228 KB
testcase_20 AC 109 ms
40,624 KB
testcase_21 AC 122 ms
41,568 KB
testcase_22 AC 107 ms
40,772 KB
testcase_23 AC 122 ms
41,212 KB
testcase_24 AC 1,230 ms
69,196 KB
testcase_25 AC 1,274 ms
76,776 KB
testcase_26 AC 1,309 ms
69,560 KB
testcase_27 AC 1,313 ms
76,712 KB
testcase_28 AC 1,296 ms
71,712 KB
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 AC 5,745 ms
219,552 KB
testcase_33 AC 5,678 ms
219,804 KB
testcase_34 AC 5,792 ms
219,904 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) {
          par[x] = y;
      }
      }
    }
  static boolean same(int x, int y) {
      return find(x) == find(y);
    }
}
0