結果

問題 No.348 カゴメカゴメ
ユーザー pekempeypekempey
提出日時 2016-02-26 23:25:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 2,684 bytes
コンパイル時間 1,865 ms
コンパイル使用メモリ 178,676 KB
実行使用メモリ 125,852 KB
最終ジャッジ日時 2023-10-24 08:35:28
合計ジャッジ時間 6,838 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
118,340 KB
testcase_01 AC 36 ms
99,876 KB
testcase_02 AC 84 ms
124,744 KB
testcase_03 AC 102 ms
118,348 KB
testcase_04 AC 36 ms
99,868 KB
testcase_05 AC 36 ms
99,896 KB
testcase_06 AC 36 ms
99,864 KB
testcase_07 AC 36 ms
99,860 KB
testcase_08 AC 36 ms
99,856 KB
testcase_09 AC 36 ms
99,868 KB
testcase_10 AC 36 ms
99,864 KB
testcase_11 AC 35 ms
99,864 KB
testcase_12 AC 38 ms
100,216 KB
testcase_13 AC 37 ms
100,000 KB
testcase_14 AC 37 ms
99,896 KB
testcase_15 AC 65 ms
109,272 KB
testcase_16 AC 36 ms
99,860 KB
testcase_17 AC 37 ms
99,860 KB
testcase_18 AC 37 ms
99,860 KB
testcase_19 AC 37 ms
99,860 KB
testcase_20 AC 37 ms
99,872 KB
testcase_21 AC 36 ms
99,864 KB
testcase_22 AC 37 ms
99,864 KB
testcase_23 AC 105 ms
125,852 KB
testcase_24 AC 109 ms
125,844 KB
testcase_25 AC 105 ms
125,588 KB
testcase_26 AC 106 ms
125,848 KB
testcase_27 AC 109 ms
125,844 KB
testcase_28 AC 106 ms
125,848 KB
testcase_29 AC 105 ms
125,848 KB
testcase_30 AC 105 ms
125,848 KB
testcase_31 AC 108 ms
125,848 KB
testcase_32 AC 105 ms
125,852 KB
testcase_33 AC 42 ms
103,120 KB
testcase_34 AC 41 ms
103,120 KB
testcase_35 AC 42 ms
103,120 KB
testcase_36 AC 43 ms
103,120 KB
testcase_37 AC 42 ms
103,120 KB
testcase_38 AC 42 ms
103,120 KB
testcase_39 AC 42 ms
103,120 KB
testcase_40 AC 42 ms
103,120 KB
testcase_41 AC 43 ms
103,120 KB
testcase_42 AC 43 ms
102,860 KB
testcase_43 AC 37 ms
100,128 KB
testcase_44 AC 37 ms
100,124 KB
testcase_45 AC 37 ms
100,128 KB
testcase_46 AC 38 ms
100,128 KB
testcase_47 AC 38 ms
100,120 KB
testcase_48 AC 37 ms
100,112 KB
testcase_49 AC 39 ms
100,132 KB
testcase_50 AC 38 ms
100,124 KB
testcase_51 AC 38 ms
100,124 KB
testcase_52 AC 38 ms
100,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define repr(i, a) for (int i = (a) - 1; i >= 0; i--)
#define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--)
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
typedef long long ll;

struct UnionFind {
	vector<int> parent, size;
	UnionFind(int n) : parent(n), size(n, 1) { for (int i = 0; i < n; i++) parent[i] = i; }
	int operator[](int x) { return parent[x] == x ? x : (parent[x] = operator[](parent[x])); }
	bool merge(int x, int y) {
		if ((x = operator[](x)) == (y = operator[](y))) return false;
		parent[x] = parent[y] = size[x] > size[y] ? parent[x] : parent[y];
		size[x] = size[y] = size[x] + size[y];
		return true;
	}
};

int dy[] = { 1, 1, 1, 0, 0, -1, -1, -1 };
int dx[] = { 1, 0, -1, 1, -1, 1, 0, -1 };

int dy2[] = { 1, 0, -1, 0 };
int dx2[] = { 0, -1, 0, 1 };

set<int> G[1010101];
set<int> rG[1010101];
ll dp[1010101][2];

void dfs(int curr, int prev, UnionFind &uf) {
	dp[curr][1] += uf.size[curr];
	for (int next : G[curr]) if (next != prev && next != curr) {
		dfs(next, curr, uf);
		dp[curr][0] += max(dp[next][0], dp[next][1]);
		dp[curr][1] += dp[next][0];
	}
}

int main() {
	int h, w;
	cin >> h >> w;
	vector<string> g(h);
	rep(i, h) cin >> g[i];

	UnionFind uf(h * w);

	rep(y, h) rep(x, w) if (g[y][x] == 'x') {
		rep(k, 8) {
			int ny = y + dy[k];
			int nx = x + dx[k];
			if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
			if (g[ny][nx] != 'x') continue;
			uf.merge(y * w + x, ny * w + nx);
		}
	}

	static bool used[1010101];
	static bool vis[1010][1010];
	rep(i, h) rep(j, w) if (g[i][j] == 'x') {
		int rt = uf[i * w + j];
		if (used[rt]) continue;
		used[rt] = true;
		int y = i;
		int x = j;
		if (g[y + 1][x] == 'x') continue;
		if (x + 1 < w && g[y][x + 1] == 'x' && g[y + 1][x + 1] == 'x') continue;
		y++;

		queue<pair<int, int>> q;
		q.emplace(y, x);
		vis[y][x] = true;
		while (!q.empty()) {
			tie(y, x) = q.front(); q.pop();
			rep(k, 4) {
				int ny = y + dy2[k];
				int nx = x + dx2[k];
				if (g[ny][nx] == 'x') {
					if (rt != uf[ny * w + nx]) {
						int nn = uf[ny * w + nx];
						G[rt].insert(nn);
						rG[nn].insert(rt);
					}
				} else if (chmax(vis[ny][nx], true)) {
					q.emplace(ny, nx);
				}
			}
		}
	}

	ll ans = 0;
	rep(i, h) rep(j, w) {
		int c = i * w + j;
		if (uf[c] != c) continue;
		if (g[i][j] == 'x' && rG[c].size() == 0) {
			dfs(c, -1, uf);
			ans += max(dp[c][0], dp[c][1]);
		}
	}
	cout << ans << endl;
	
	return 0;
}
0