結果

問題 No.348 カゴメカゴメ
ユーザー pekempeypekempey
提出日時 2016-02-26 23:18:32
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,381 bytes
コンパイル時間 1,727 ms
コンパイル使用メモリ 181,572 KB
実行使用メモリ 125,020 KB
最終ジャッジ日時 2023-10-24 06:37:02
合計ジャッジ時間 7,550 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
118,624 KB
testcase_01 AC 37 ms
101,128 KB
testcase_02 AC 90 ms
125,020 KB
testcase_03 AC 131 ms
118,884 KB
testcase_04 AC 37 ms
101,124 KB
testcase_05 AC 37 ms
101,152 KB
testcase_06 AC 36 ms
101,116 KB
testcase_07 AC 36 ms
101,112 KB
testcase_08 AC 36 ms
101,112 KB
testcase_09 AC 36 ms
101,120 KB
testcase_10 WA -
testcase_11 AC 36 ms
101,116 KB
testcase_12 AC 37 ms
101,444 KB
testcase_13 WA -
testcase_14 AC 36 ms
101,148 KB
testcase_15 AC 67 ms
110,340 KB
testcase_16 AC 36 ms
101,112 KB
testcase_17 AC 36 ms
101,112 KB
testcase_18 AC 37 ms
101,116 KB
testcase_19 AC 37 ms
101,116 KB
testcase_20 AC 37 ms
101,124 KB
testcase_21 AC 37 ms
101,116 KB
testcase_22 AC 37 ms
101,116 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 AC 39 ms
101,372 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 38 ms
101,368 KB
testcase_51 WA -
testcase_52 WA -
権限があれば一括ダウンロードができます

ソースコード

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 + 1;
		int x = j;
		if (g[y][x] == 'x') continue;

		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 (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
				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);
				}
			}
		}
	}
	memset(used, 0, sizeof(used));
	memset(vis, 0, sizeof(vis));
	rep(j, w) rep(i, h) if (g[i][j] == 'x') {
		int rt = uf[i * w + j];
		if (used[rt]) continue;
		used[rt] = true;
		int y = i;
		int x = j + 1;
		if (g[y][x] == 'x') continue;

		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 (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
				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