結果
問題 | No.697 池の数はいくつか |
ユーザー |
|
提出日時 | 2018-06-11 11:25:15 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 1,490 ms / 6,000 ms |
コード長 | 1,459 bytes |
コンパイル時間 | 1,013 ms |
コンパイル使用メモリ | 118,656 KB |
実行使用メモリ | 166,016 KB |
最終ジャッジ日時 | 2024-06-13 01:12:35 |
合計ジャッジ時間 | 13,189 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 32 |
ソースコード
import std.stdio, std.array, std.string, std.conv, std.algorithm;import std.typecons, std.range, std.random, std.math, std.container;import std.numeric, std.bigint, core.bitop, std.bitmanip;void main() {auto s = readln.split.map!(to!int);auto H = s[0];auto W = s[1];auto A = H.iota.map!(_ => readln.split.map!(to!int).array).array;const int[] dr = [0, 0, -1, 1];const int[] dc = [-1, 1, 0, 0];auto uf = new UnionFind(H*W);auto used = new bool[](H*W);int ans = 0;foreach (i; 0..H)foreach (j; 0..W)foreach (k; 0..4)if (i+dr[k] >= 0 && i+dr[k] < H)if (j+dc[k] >= 0 && j + dc[k] < W)if (A[i][j] == 1 && A[i+dr[k]][j+dc[k]] == 1)uf.unite(i*W+j, (i+dr[k])*W+j+dc[k]);foreach (i; 0..H)foreach (j; 0..W)if (A[i][j] == 1 && !used[uf.find(i*W+j)])ans += 1, used[uf.find(i*W+j)] = true;ans.writeln;}class UnionFind {int N;int[] table;this(int n) {N = n;table = new int[](N);fill(table, -1);}int find(int x) {return table[x] < 0 ? x : (table[x] = find(table[x]));}void unite(int x, int y) {x = find(x);y = find(y);if (x == y) return;if (table[x] > table[y]) swap(x, y);table[x] += table[y];table[y] = x;}}