結果
問題 |
No.697 池の数はいくつか
|
ユーザー |
![]() |
提出日時 | 2019-07-24 20:03:22 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,682 bytes |
コンパイル時間 | 2,362 ms |
コンパイル使用メモリ | 77,416 KB |
実行使用メモリ | 220,272 KB |
最終ジャッジ日時 | 2024-11-25 15:04:19 |
合計ジャッジ時間 | 50,961 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 TLE * 5 |
ソースコード
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); } }