結果

問題 No.697 池の数はいくつか
ユーザー bal4ubal4u
提出日時 2019-07-12 17:21:31
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 238 ms / 6,000 ms
コード長 1,444 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 30,224 KB
実行使用メモリ 72,156 KB
最終ジャッジ日時 2023-08-08 02:32:15
合計ジャッジ時間 3,947 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 28 ms
11,960 KB
testcase_25 AC 29 ms
11,916 KB
testcase_26 AC 28 ms
12,008 KB
testcase_27 AC 28 ms
11,952 KB
testcase_28 AC 28 ms
12,024 KB
testcase_29 AC 176 ms
72,156 KB
testcase_30 AC 235 ms
72,076 KB
testcase_31 AC 177 ms
71,996 KB
testcase_32 AC 238 ms
72,108 KB
testcase_33 AC 236 ms
72,080 KB
testcase_34 AC 236 ms
72,128 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:7:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    7 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:13:24: 備考: in expansion of macro ‘gc’
   13 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// 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;
}
0