結果
| 問題 | No.348 カゴメカゴメ |
| コンテスト | |
| ユーザー |
mayoko_
|
| 提出日時 | 2016-02-27 14:14:12 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 4,437 bytes |
| 記録 | |
| コンパイル時間 | 1,205 ms |
| コンパイル使用メモリ | 105,352 KB |
| 実行使用メモリ | 57,236 KB |
| 最終ジャッジ日時 | 2024-09-24 11:35:35 |
| 合計ジャッジ時間 | 7,221 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 52 WA * 1 |
ソースコード
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
#include<complex>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
struct UnionFind {
vector<int> par;
int n, cnt;
UnionFind(const int& x = 0) {init(x);}
void init(const int& x) {par.assign(cnt=n=x, -1);}
inline int find(const int& x) {return par[x] < 0 ? x : par[x] = find(par[x]);}
inline bool same(const int& x, const int& y) {return find(x) == find(y);}
inline bool unite(int x, int y) {
if ((x = find(x)) == (y = find(y))) return false;
--cnt;
if (par[x] > par[y]) swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
inline int count() const {return cnt;}
inline int count(int x) {return -par[find(x)];}
};
const int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddx[] = {1, 0, -1, 0};
const int ddy[] = {0, 1, 0, -1};
string grid[1010];
bool done[1030*1030];
vector<int> G[1010*1010];
int dp[1010*1010][2];
// flag: 1 ならそのスコアを加算しても良い
// flag: 0 ならスコアを加算しちゃダメ
int dfs(int v, int flag, const vector<int>& score) {
int& ret = dp[v][flag];
if (ret >= 0) return ret;
ret = 0;
if (score[v] == 0) {
for (int ch : G[v]) {
ret += dfs(ch, flag, score);
}
} else {
if (flag == 0) {
for (int ch : G[v]) {
ret += dfs(ch, 1, score);
}
} else {
ret = score[v];
for (int ch : G[v]) {
ret += dfs(ch, 0, score);
}
int tmp = 0;
for (int ch : G[v]) {
tmp += dfs(ch, 1, score);
}
ret = max(tmp, ret);
}
}
return ret;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
// 周りを . で囲みつつ input
for (int i = 0; i < M+2; i++) grid[0] += '.';
for (int i = 1; i <= N; i++) {
cin >> grid[i];
grid[i] = "." + grid[i] + ".";
}
for (int i = 0; i < M+2; i++) grid[0] += '.';
N += 2; M += 2;
UnionFind uf(N*M);
// 'x'
for (int y = 0; y < N; y++) for (int x = 0; x < M; x++) {
if (grid[y][x] != 'x') continue;
for (int k = 0; k < 8; k++) {
int ny = y+dy[k], nx = x+dx[k];
if (ny < 0 || ny >= N || nx < 0 || nx >= M) continue;
if (grid[ny][nx] == 'x') uf.unite(y*M+x, ny*M+nx);
}
}
// '.'
for (int y = 0; y < N; y++) for (int x = 0; x < M; x++) {
if (grid[y][x] == 'x') continue;
for (int k = 0; k < 4; k++) {
int ny = y+ddy[k], nx = x+ddx[k];
if (ny < 0 || ny >= N || nx < 0 || nx >= M) continue;
if (grid[ny][nx] != 'x') uf.unite(y*M+x, ny*M+nx);
}
}
// 木を作っていく
map<int, int> trans;
for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) {
trans[uf.find(i*M+j)] = 0;
}
int k = 0;
for (auto& p : trans) p.second = k++;
vector<int> score(k);
for (auto p : trans) {
int y = p.first/M, x = p.first%M;
if (grid[y][x] == '.') score[p.second] = 0;
else score[p.second] = uf.count(p.first);
}
vector<vi> group(k);
for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) {
group[trans[uf.find(i*M+j)]].push_back(i*M+j);
}
// 木を作る dfs
function<void(int)> f = [&] (int v) {
done[v] = true;
for (int el : group[v]) {
int y = el/M, x = el%M;
for (int k = 0; k < 4; k++) {
int ny = y+ddy[k], nx = x+ddx[k];
if (ny < 0 || ny >= N || nx < 0 || nx >= M) continue;
int next = trans[uf.find(ny*M+nx)];
if (done[next]) continue;
G[v].push_back(next);
f(next);
}
}
};
f(0);
// 木DP
memset(dp, -1, sizeof(dp));
cout << dfs(trans[0], 1, score) << endl;
return 0;
}
mayoko_