結果

問題 No.348 カゴメカゴメ
ユーザー togari_takamototogari_takamoto
提出日時 2016-02-27 01:05:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 3,503 bytes
コンパイル時間 1,876 ms
コンパイル使用メモリ 182,412 KB
実行使用メモリ 35,016 KB
最終ジャッジ日時 2023-10-24 18:07:58
合計ジャッジ時間 5,139 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
14,328 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 177 ms
28,328 KB
testcase_03 AC 116 ms
35,016 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 4 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 61 ms
20,020 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 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 2 ms
4,348 KB
testcase_23 AC 117 ms
12,248 KB
testcase_24 AC 113 ms
11,288 KB
testcase_25 AC 117 ms
13,252 KB
testcase_26 AC 117 ms
12,872 KB
testcase_27 AC 112 ms
11,000 KB
testcase_28 AC 117 ms
12,424 KB
testcase_29 AC 114 ms
11,552 KB
testcase_30 AC 116 ms
12,028 KB
testcase_31 AC 112 ms
11,784 KB
testcase_32 AC 115 ms
11,368 KB
testcase_33 AC 10 ms
4,388 KB
testcase_34 AC 10 ms
4,412 KB
testcase_35 AC 10 ms
4,408 KB
testcase_36 AC 10 ms
4,348 KB
testcase_37 AC 11 ms
4,468 KB
testcase_38 AC 11 ms
4,480 KB
testcase_39 AC 11 ms
4,392 KB
testcase_40 AC 11 ms
4,432 KB
testcase_41 AC 11 ms
4,444 KB
testcase_42 AC 10 ms
4,448 KB
testcase_43 AC 3 ms
4,348 KB
testcase_44 AC 3 ms
4,348 KB
testcase_45 AC 3 ms
4,348 KB
testcase_46 AC 3 ms
4,348 KB
testcase_47 AC 3 ms
4,348 KB
testcase_48 AC 3 ms
4,348 KB
testcase_49 AC 3 ms
4,348 KB
testcase_50 AC 3 ms
4,348 KB
testcase_51 AC 3 ms
4,348 KB
testcase_52 AC 3 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘ll ring_dfs(const vvb&, ll, ll, ll, ll, vvb&)’:
main.cpp:63:1: warning: control reaches end of non-void function [-Wreturn-type]
   63 | }
      | ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi  = vector<int>; using vb  = vector<bool>; using vd  = vector<double>; using vl  = vector<ll>;
using vvi = vector<vi>;  using vvb = vector<vb>;   using vvd = vector<vd>;     using vvl = vector<vl>;

#define REP(i,n) for(ll i=0; i<(n); ++i)
#define FOR(i,b,n) for(ll i=(b); i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define TEN(x) ((ll)1e##x)

struct Node{
	ll size = 0;
	vector<Node> children;
};

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

bool contain(ll n, ll x) { return 0 <= x && x < n; }

void blank_dfs(const vvb & table, ll x, ll y, vvb & visited, set<pair<ll, ll>> & discover) {
	ll n = table.size(); ll m = table[0].size();
	
	stack<pair<ll, ll>> st; st.push({ x, y });
	while (!st.empty()) {
		ll x = st.top().first; ll y = st.top().second; st.pop();
		if (visited[y][x]) continue;
		visited[y][x] = true;

		REP(i, 4) {
			ll nx = x + dx[i]; ll ny = y + dy[i];
			if (!contain(m, nx) || !contain(n, ny)) continue;
			if (visited[ny][nx]) continue;
			if (table[ny][nx]) discover.insert({ nx, ny });
			else st.push({ nx, ny });
		}
	}
}

Node ring_search(const vvb & table, ll x, ll y, vvb & visited);
void blank_search(const vvb & table, ll x, ll y, vvb & visited, vector<Node> & nodes) {
	set<pair<ll, ll>> discover;
	blank_dfs(table, x, y, visited, discover);

	for (auto pos : discover) {
		if (visited[pos.second][pos.first]) continue;
		nodes.emplace_back(ring_search(table, pos.first, pos.second, visited));
	}
}

ll ring_dfs(const vvb & table,ll x, ll y, ll px, ll py, vvb & visited) {
    ll n = table.size(); ll m = table[0].size();
    if (visited[y][x]) return 0;
    visited[y][x] = true;
    
    REP(i, 8) {
        ll nx = x + dx[i]; ll ny = y + dy[i];
        if (!contain(m, nx) || !contain(n, ny)) continue;
        if (nx == px && ny == py) continue;
        if (table[ny][nx]) return 1 + ring_dfs(table, nx, ny, x, y, visited);
    }
}

Node ring_search(const vvb & table, ll x, ll y, vvb & visited) {
	ll n = table.size(); ll m = table[0].size();
	Node node;
	node.size = ring_dfs(table, x, y, -1, -1, visited);
	REP(i, 8) {
		ll nx = x + dx[i]; ll ny = y + dy[i];
		if (!contain(m, nx) || !contain(n, ny)) continue;
		if (!visited[ny][nx] && !table[ny][nx]) {
			blank_search(table, nx, ny, visited, node.children);
			return node;
		}
	}
	return node;
}

pair<ll,ll> dfs(Node & node) {
	auto ans = make_pair(node.size, 0);
	for (auto&& child : node.children) {
		auto _ = dfs(child);
		ans.first += _.second;
		ans.second += max(_.first, _.second);
	}
	return ans;
}

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	ll n, m;
	cin >> n >> m;
	vvb table(n, vb(m));
	REP(i, n) REP(j, m) {
		char c; cin >> c;
		table[i][j] = c!='.';
	}

	vvb visited(n, vb(m, false));
	vector<Node> nodes;
	REP(y, n) {
		set<pair<ll, ll>> _;
		if (!visited[y][0] && !table[y][0]) blank_dfs(table, 0, y, visited, _);
		if (!visited[y][m-1] && !table[y][m-1]) blank_dfs(table, m-1, y, visited, _);
	}REP(x, m) {
		set<pair<ll, ll>> _;
		if (!visited[0][x] && !table[0][x]) blank_dfs(table, x, 0, visited, _);
		if (!visited[n-1][x] && !table[n-1][x]) blank_dfs(table, x, n-1, visited, _);
	}

	REP(y, n) REP(x, m){
		if (visited[y][x]) continue;
		nodes.emplace_back(ring_search(table, x, y, visited));
	}

	ll ans = 0;
	for (auto&& child : nodes) {
		auto _ = dfs(child);
		ans += max(_.first, _.second);
	}

	cout << ans << endl;

	return 0;
}
0