結果

問題 No.697 池の数はいくつか
ユーザー takeya_okinotakeya_okino
提出日時 2019-08-20 21:23:59
言語 Java19
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,679 bytes
コンパイル時間 4,633 ms
コンパイル使用メモリ 74,324 KB
実行使用メモリ 265,372 KB
最終ジャッジ日時 2023-08-16 23:14:10
合計ジャッジ時間 54,269 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
55,660 KB
testcase_01 AC 130 ms
55,692 KB
testcase_02 AC 130 ms
55,904 KB
testcase_03 AC 131 ms
55,372 KB
testcase_04 AC 133 ms
55,676 KB
testcase_05 AC 129 ms
55,888 KB
testcase_06 AC 132 ms
55,832 KB
testcase_07 AC 128 ms
55,688 KB
testcase_08 AC 128 ms
55,628 KB
testcase_09 AC 128 ms
55,648 KB
testcase_10 AC 135 ms
55,568 KB
testcase_11 AC 129 ms
55,848 KB
testcase_12 AC 127 ms
55,828 KB
testcase_13 AC 135 ms
55,812 KB
testcase_14 AC 133 ms
55,436 KB
testcase_15 AC 129 ms
55,500 KB
testcase_16 AC 128 ms
55,624 KB
testcase_17 AC 130 ms
55,428 KB
testcase_18 AC 133 ms
55,324 KB
testcase_19 AC 129 ms
55,624 KB
testcase_20 AC 133 ms
55,652 KB
testcase_21 AC 130 ms
55,928 KB
testcase_22 AC 127 ms
55,524 KB
testcase_23 AC 136 ms
55,568 KB
testcase_24 AC 1,440 ms
89,308 KB
testcase_25 AC 1,384 ms
87,996 KB
testcase_26 AC 1,347 ms
84,056 KB
testcase_27 AC 1,407 ms
89,796 KB
testcase_28 AC 1,339 ms
82,056 KB
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 AC 5,982 ms
262,300 KB
testcase_33 TLE -
testcase_34 TLE -
権限があれば一括ダウンロードができます

ソースコード

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