結果

問題 No.348 カゴメカゴメ
ユーザー antaanta
提出日時 2016-02-26 22:50:33
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,229 bytes
コンパイル時間 970 ms
コンパイル使用メモリ 96,868 KB
実行使用メモリ 51,496 KB
最終ジャッジ日時 2023-10-24 05:34:56
合計ジャッジ時間 5,329 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 147 ms
47,328 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 183 ms
49,304 KB
testcase_03 AC 157 ms
51,496 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 6 ms
4,456 KB
testcase_13 AC 4 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 125 ms
40,464 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 1 ms
4,348 KB
testcase_23 WA -
testcase_24 AC 164 ms
46,448 KB
testcase_25 AC 166 ms
47,016 KB
testcase_26 AC 165 ms
46,924 KB
testcase_27 AC 164 ms
46,596 KB
testcase_28 AC 166 ms
47,116 KB
testcase_29 AC 165 ms
46,728 KB
testcase_30 AC 165 ms
46,820 KB
testcase_31 AC 165 ms
46,648 KB
testcase_32 AC 165 ms
46,716 KB
testcase_33 AC 16 ms
6,996 KB
testcase_34 AC 15 ms
7,032 KB
testcase_35 AC 16 ms
7,028 KB
testcase_36 AC 16 ms
6,832 KB
testcase_37 AC 16 ms
6,984 KB
testcase_38 AC 15 ms
6,952 KB
testcase_39 AC 16 ms
6,832 KB
testcase_40 AC 16 ms
6,968 KB
testcase_41 WA -
testcase_42 AC 16 ms
7,028 KB
testcase_43 AC 4 ms
4,348 KB
testcase_44 WA -
testcase_45 AC 4 ms
4,348 KB
testcase_46 AC 4 ms
4,348 KB
testcase_47 AC 4 ms
4,348 KB
testcase_48 WA -
testcase_49 AC 3 ms
4,348 KB
testcase_50 AC 4 ms
4,348 KB
testcase_51 WA -
testcase_52 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

struct UnionFind {
	vector<int> data;
	void init(int n) { data.assign(n, -1); }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if(x != y) {
			if(data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) { return root(x) == root(y); }
	int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
	int size(int x) { return -data[root(x)]; }
};

vector<int> memo;
int rec(int i, int p, bool b, const vector<vi> &g, const vector<string> &grid, UnionFind &uf) {
	int &r = memo[i * 2 + b];
	if(r != -1) return r;
	if(grid[i / grid[0].size()][i % grid[0].size()] == '.') {
		r = 0;
		each(j, g[i]) if(*j != p)
			r += rec(*j, i, b, g, grid, uf);
	} else {
		int t = 0, u = 0;
		each(j, g[i]) if(*j != p) {
			t += rec(*j, i, false, g, grid, uf);
			u += rec(*j, i, true, g, grid, uf);
		}
		r = t;
		if(!b)
			amax(r, u + uf.size(i));
	}
	return r;
}

int main() {
	int N; int M;
	while(~scanf("%d%d", &N, &M)) {
		vector<string> grid(N + 1);
		rep(i, N)
			cin >> grid[i];
		grid[N ++] = string(M, '.');
		UnionFind uf; uf.init(N * M);
		rep(i, N) rep(j, M) {
			rer(dy, -1, 1) rer(dx, -1, 1) if(dy || dx) {
				int yy = i + dy, xx = j + dx;
				if(yy == -1 || xx == -1 || yy == N || xx == M) continue;
				if(grid[i][j] == '.' && abs(dy) + abs(dx) > 1) continue;
				if(grid[i][j] != grid[yy][xx]) {
					continue;
				}
				uf.unionSet(i * M + j, yy * M + xx);
			}
		}
		vector<vi> g(N * M);
		rep(i, N) rep(j, M) {
			rer(dy, -1, 1) rer(dx, -1, 1) if(dy || dx) {
				int yy = i + dy, xx = j + dx;
				if(yy == -1 || xx == -1 || yy == N || xx == M) continue;
				if(abs(dy) + abs(dx) > 1) continue;
				if(grid[i][j] != grid[yy][xx]) {
					g[uf.root(i * M + j)].push_back(uf.root(yy * M + xx));
					continue;
				}
			}
		}
		rep(i, N * M) {
			vi &v = g[i];
			sort(v.begin(), v.end());
			v.erase(unique(v.begin(), v.end()), v.end());
		}
		memo.assign(N * M * 2, -1);
		int ans = rec(uf.root(N * M - 1), -1, false, g, grid, uf);
		printf("%d\n", ans);
	}
	return 0;
}
0