結果

問題 No.697 池の数はいくつか
ユーザー bal4ubal4u
提出日時 2019-07-12 17:01:00
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 242 ms / 6,000 ms
コード長 1,417 bytes
コンパイル時間 1,793 ms
コンパイル使用メモリ 31,872 KB
実行使用メモリ 72,144 KB
最終ジャッジ日時 2024-04-25 20:31:14
合計ジャッジ時間 4,040 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 1 ms
6,812 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 26 ms
12,364 KB
testcase_25 AC 25 ms
10,308 KB
testcase_26 AC 25 ms
10,288 KB
testcase_27 AC 25 ms
13,060 KB
testcase_28 AC 25 ms
13,140 KB
testcase_29 AC 154 ms
71,988 KB
testcase_30 AC 242 ms
72,012 KB
testcase_31 AC 155 ms
71,996 KB
testcase_32 AC 229 ms
72,108 KB
testcase_33 AC 223 ms
72,000 KB
testcase_34 AC 219 ms
72,144 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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();
      |                        ^~

ソースコード

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, p0, 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]) land++;
		else if (c && (t[0][c] & t[0][c-1])) unite(c, c-1);
	}
	p0 = 0, p = W, k0 = 0, k = 1;
	for (r = 1; r < H; r++) {
		for (c = 0; c < W; c++) {
			t[k][c] = gc() & 1, gc();
			if (!t[k][c]) land++;
			else {
				if (t[k][c] & t[k0][c]) unite(p0+c, p+c);
				if (c > 0 && (t[k][c] & t[k][c-1])) unite(p+c, p+c-1);
			}
		}
		p0 += W, p += W, k0 = k, k = !k;
	}
	
	ans = 0;
	k = H*W; while (k--) if (root(k) == k) ans++;
	printf("%d\n", ans - land);
	return 0;
}
0