結果

問題 No.348 カゴメカゴメ
ユーザー FF256grhyFF256grhy
提出日時 2017-10-26 06:37:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 578 ms / 2,000 ms
コード長 3,880 bytes
コンパイル時間 2,087 ms
コンパイル使用メモリ 185,980 KB
実行使用メモリ 176,060 KB
最終ジャッジ日時 2024-05-01 14:48:36
合計ジャッジ時間 9,925 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 280 ms
124,528 KB
testcase_01 AC 11 ms
27,100 KB
testcase_02 AC 411 ms
140,272 KB
testcase_03 AC 578 ms
176,060 KB
testcase_04 AC 10 ms
27,200 KB
testcase_05 AC 10 ms
27,384 KB
testcase_06 AC 9 ms
27,224 KB
testcase_07 AC 8 ms
27,084 KB
testcase_08 AC 7 ms
27,172 KB
testcase_09 AC 9 ms
27,136 KB
testcase_10 AC 7 ms
27,076 KB
testcase_11 AC 8 ms
27,068 KB
testcase_12 AC 15 ms
29,184 KB
testcase_13 AC 11 ms
27,804 KB
testcase_14 AC 9 ms
27,348 KB
testcase_15 AC 162 ms
96,028 KB
testcase_16 AC 10 ms
27,080 KB
testcase_17 AC 8 ms
27,168 KB
testcase_18 AC 8 ms
27,096 KB
testcase_19 AC 8 ms
27,028 KB
testcase_20 AC 9 ms
27,244 KB
testcase_21 AC 10 ms
26,976 KB
testcase_22 AC 9 ms
27,092 KB
testcase_23 AC 366 ms
143,280 KB
testcase_24 AC 394 ms
143,852 KB
testcase_25 AC 363 ms
143,040 KB
testcase_26 AC 361 ms
143,028 KB
testcase_27 AC 363 ms
143,360 KB
testcase_28 AC 370 ms
143,676 KB
testcase_29 AC 363 ms
143,072 KB
testcase_30 AC 383 ms
143,456 KB
testcase_31 AC 359 ms
143,140 KB
testcase_32 AC 353 ms
143,536 KB
testcase_33 AC 39 ms
35,884 KB
testcase_34 AC 35 ms
35,880 KB
testcase_35 AC 37 ms
35,884 KB
testcase_36 AC 39 ms
35,888 KB
testcase_37 AC 37 ms
35,752 KB
testcase_38 AC 37 ms
35,888 KB
testcase_39 AC 37 ms
36,272 KB
testcase_40 AC 39 ms
35,888 KB
testcase_41 AC 38 ms
35,760 KB
testcase_42 AC 37 ms
35,752 KB
testcase_43 AC 13 ms
27,980 KB
testcase_44 AC 12 ms
28,120 KB
testcase_45 AC 13 ms
28,036 KB
testcase_46 AC 13 ms
28,280 KB
testcase_47 AC 13 ms
28,112 KB
testcase_48 AC 13 ms
28,032 KB
testcase_49 AC 12 ms
28,036 KB
testcase_50 AC 12 ms
28,412 KB
testcase_51 AC 12 ms
28,108 KB
testcase_52 AC 11 ms
28,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define  inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define  dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)

template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

struct UnionFind {
	int s, num; // [0, s) の s 個の要素がある。num は非空集合の個数。
	vector<vector<int>> v;
	vector<int> find;
	
	UnionFind() { s = 0; }
	UnionFind(int s) { init(s); }
	void init(int arg_s) {
		num = s = arg_s;
		v.clear();
		find.clear();
		inc(i, s) { v.PB({i}); find.PB(i); }
	}
	bool unite(int x, int y) {
		assert(inID(x, 0, s) && inID(y, 0, s));
		if(find[x] == find[y]) { return false; }
		num--;
		if(v[find[x]].size() > v[find[y]].size()) { swap(x, y); }
		int fx = find[x];
		for(auto && e: v[find[x]]) { v[find[y]].PB(e); find[e] = find[y]; }
		v[fx].clear();
		return true;
	}
};

// ----

#define UQ(v) v.erase(unique(ALL(v)), v.end())

int n, m;
string s[1003];
UnionFind uf;
vector<int> v[1003 * 1003];

pair<int, int> dfs(int pos, int par, int t) {
	auto p1 = MP(0, 0);
	inc(i, v[pos].size()) {
		if(v[pos][i] == par) { continue; }
		auto p2 = dfs(v[pos][i], pos, 1 - t);
		p1.FI += p2.FI;
		p1.SE += p2.SE;
	}
	if(t == 0) { return p1; }
	swap(p1.FI, p1.SE);
	p1.FI += uf.v[pos].size();
	setmax(p1.FI, p1.SE);
	return p1;
}

int main() {
	cin >> n >> m;
	s[0] = s[1] = s[n + 2] = string(m + 3, '.');
	incID(i, 2, n + 2) { cin >> s[i]; s[i] = ".." + s[i] + "."; }
	s[0][0] = 'x';
	
	n += 3;
	m += 3;
	uf.init(n * m);
	inc(i, n) {
	inc(j, m) {
		if(s[i][j] != 'x') { continue; }
		incII(ii, i - 1, i + 1) { if(! inID(ii, 0, n)) { continue; }
		incII(jj, j - 1, j + 1) { if(! inID(jj, 0, m)) { continue; }
			if(s[ii][jj] == 'x') { uf.unite(m * i + j, m * ii + jj); }
		}
		}
	}
	}
	inc(i, n) {
	inc(j, m) {
		if(s[i][j] != '.') { continue; }
		int di[4] = { 1, 0, -1,  0 };
		int dj[4] = { 0, 1,  0, -1 };
		inc(k, 4) {
			int ii = i + di[k];
			int jj = j + dj[k];
			if(! (inID(ii, 0, n) && inID(jj, 0, m))) { continue; }
			if(s[ii][jj] == '.') { uf.unite(m * i + j, m * ii + jj); }
		}
	}
	}
	vector<pair<int, int>> vp;
	inc(i, n) {
	inc(j, m) {
		if(s[i][j] != 'x') { continue; }
		incII(ii, i - 1, i + 1) { if(! inID(ii, 0, n)) { continue; }
		incII(jj, j - 1, j + 1) { if(! inID(jj, 0, m)) { continue; }
			if(s[ii][jj] == '.') {
				vp.EB(uf.find[m * i + j], uf.find[m * ii + jj]);
				vp.EB(uf.find[m * ii + jj], uf.find[m * i + j]);
			}
		}
		}
	}
	}
	sort(ALL(vp));
	UQ(vp);
	inc(i, vp.size()) { v[vp[i].FI].PB(vp[i].SE); }
	
	cout << dfs(uf.find[m * 0 + 0], -1, 1).SE << endl;
	
	return 0;
}
0