結果
問題 | No.697 池の数はいくつか |
ユーザー |
![]() |
提出日時 | 2019-07-12 17:21:31 |
言語 | C (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 276 ms / 6,000 ms |
コード長 | 1,444 bytes |
コンパイル時間 | 201 ms |
コンパイル使用メモリ | 30,976 KB |
実行使用メモリ | 72,192 KB |
最終ジャッジ日時 | 2024-11-08 08:19:25 |
合計ジャッジ時間 | 3,896 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 32 |
コンパイルメッセージ
main.c: In function 'in': main.c:7:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration] 7 | #define gc() getchar_unlocked() | ^~~~~~~~~~~~~~~~ main.c:13:24: note: in expansion of macro 'gc' 13 | int n = 0, c = gc(); | ^~
ソースコード
// yukicoder: No.697 池の数はいくつか // 2019.7.12 bal4u #include <stdio.h> #if 1 #define gc() getchar_unlocked() #else #define gc() getchar() #endif int in() { // 非負整数の入力 int n = 0, c = gc(); do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0'); return n; } /* UNION-FIND library */ #define MAX 9000005 int id[MAX], size[MAX]; void init(int n) { int i; for (i = 0; i < n; i++) id[i] = i, size[i] = 1; } int root(int i) { while (i != id[i]) id[i] = id[id[i]], i = id[i]; return i; } int connected(int p, int q) { return root(p) == root(q); } void unite(int p, int q) { int i = root(p), j = root(q); if (i == j) return; if (size[i] < size[j]) id[i] = j, size[j] += size[i]; else id[j] = i, size[i] += size[j]; } int H, W; char t[2][3003]; int main() { int k0, k, p, r, c, ans, land; H = in(), W = in(); init(H*W); ans = land = 0; for (c = 0; c < W; c++) { t[0][c] = gc() & 1, gc(); if (t[0][c]) { if (c && t[0][c-1]) unite(c, c-1); } else land++; } p = W, k0 = 0, k = 1; for (r = 1; r < H; r++) { t[k][0] = gc() & 1, gc(); if (t[k][0]) { if (t[k0][0]) unite(p, p-W); } else land++; p++; for (c = 1; c < W; c++) { t[k][c] = gc() & 1, gc(); if (t[k][c]) { if (t[k0][c]) unite(p, p-W); if (t[k][c-1]) unite(p, p-1); } else land++; p++; } k0 = k, k = !k; } ans = 0; k = H*W; while (k--) if (id[k] == k) ans++; printf("%d\n", ans - land); return 0; }