結果

問題 No.348 カゴメカゴメ
ユーザー chocoruskchocorusk
提出日時 2020-01-21 20:18:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 2,234 bytes
コンパイル時間 1,345 ms
コンパイル使用メモリ 119,792 KB
実行使用メモリ 61,964 KB
最終ジャッジ日時 2023-09-20 08:44:14
合計ジャッジ時間 6,450 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
41,572 KB
testcase_01 AC 3 ms
9,836 KB
testcase_02 AC 125 ms
45,848 KB
testcase_03 AC 66 ms
20,360 KB
testcase_04 AC 3 ms
9,720 KB
testcase_05 AC 5 ms
13,072 KB
testcase_06 AC 3 ms
9,492 KB
testcase_07 AC 4 ms
9,500 KB
testcase_08 AC 3 ms
9,500 KB
testcase_09 AC 4 ms
9,584 KB
testcase_10 AC 3 ms
9,576 KB
testcase_11 AC 3 ms
9,560 KB
testcase_12 AC 5 ms
10,400 KB
testcase_13 AC 3 ms
9,728 KB
testcase_14 AC 3 ms
9,520 KB
testcase_15 AC 97 ms
61,964 KB
testcase_16 AC 4 ms
9,780 KB
testcase_17 AC 3 ms
9,548 KB
testcase_18 AC 3 ms
9,600 KB
testcase_19 AC 3 ms
9,484 KB
testcase_20 AC 3 ms
9,616 KB
testcase_21 AC 3 ms
9,544 KB
testcase_22 AC 3 ms
9,512 KB
testcase_23 AC 77 ms
22,924 KB
testcase_24 AC 77 ms
22,368 KB
testcase_25 AC 78 ms
22,896 KB
testcase_26 AC 77 ms
22,448 KB
testcase_27 AC 77 ms
22,024 KB
testcase_28 AC 78 ms
22,968 KB
testcase_29 AC 78 ms
23,080 KB
testcase_30 AC 77 ms
22,696 KB
testcase_31 AC 76 ms
21,988 KB
testcase_32 AC 77 ms
22,504 KB
testcase_33 AC 9 ms
12,292 KB
testcase_34 AC 9 ms
12,436 KB
testcase_35 AC 9 ms
12,320 KB
testcase_36 AC 10 ms
12,532 KB
testcase_37 AC 9 ms
12,500 KB
testcase_38 AC 10 ms
12,296 KB
testcase_39 AC 9 ms
12,472 KB
testcase_40 AC 9 ms
12,280 KB
testcase_41 AC 9 ms
12,616 KB
testcase_42 AC 9 ms
12,576 KB
testcase_43 AC 4 ms
9,644 KB
testcase_44 AC 4 ms
9,708 KB
testcase_45 AC 4 ms
9,720 KB
testcase_46 AC 4 ms
9,912 KB
testcase_47 AC 4 ms
9,704 KB
testcase_48 AC 4 ms
9,736 KB
testcase_49 AC 4 ms
9,868 KB
testcase_50 AC 4 ms
9,676 KB
testcase_51 AC 4 ms
9,664 KB
testcase_52 AC 4 ms
9,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
int n, m;
string a[1010];
int b[1010][1010];
int c;
int dx[8]={1, -1, 0, 0, 1, 1, -1, -1}, dy[8]={0, 0, 1, -1, 1, -1, 1, -1};
void dfs(int x, int y, int v){
	b[x][y]=v;
	for(int k=0; k<4; k++){
		int x1=x+dx[k], y1=y+dy[k];
		if(x1<0 || x1>=n || y1<0 || y1>=m) continue;
		if(a[x1][y1]=='x' || b[x1][y1]>=0) continue;
		dfs(x1, y1, v);
	}
}
void dfs2(int x, int y, int v, int &cnt){
	cnt++;
	b[x][y]=v;
	for(int k=0; k<8; k++){
		int x1=x+dx[k], y1=y+dy[k];
		if(x1<0 || x1>=n || y1<0 || y1>=m) continue;
		if(b[x1][y1]>=0) continue;
		if(a[x1][y1]=='.'){
			if(k<4) dfs(x1, y1, v);
		}else dfs2(x1, y1, v, cnt);
	}
}
int cnt[1000010];
vector<vector<int>> g;
int dp[2][1000010];
void dfs3(int x, int p){
	for(auto y:g[x]){
		if(y==p) continue;
		dfs3(y, x);
		dp[1][x]+=dp[0][y];
		dp[0][x]+=max(dp[0][y], dp[1][y]);
	}
	dp[1][x]+=cnt[x];
}
int main()
{
	cin>>n>>m;
	for(int i=0; i<n+2; i++){
		for(int j=0; j<m+2; j++) a[i]+='.', b[i][j]=-1;
	}
	for(int i=1; i<=n; i++){
		string s; cin>>s;
		for(int j=1; j<=m; j++) a[i][j]=s[j-1];
	}
	n+=2, m+=2;
	dfs(0, 0, 0);
	c++;
	for(int i=0; i<n; i++){
		for(int j=0; j<m; j++){
			if(b[i][j]==-1 && a[i][j]=='x'){
				dfs2(i, j, c, cnt[c]);
				c++;
			}
		}
	}
	g.resize(c);
	for(int i=0; i<n; i++){
		for(int j=0; j<m; j++){
			if(j<m-1 && b[i][j]!=b[i][j+1]){
				g[b[i][j]].push_back(b[i][j+1]);
				g[b[i][j+1]].push_back(b[i][j]);
			}
			if(i<n-1 && b[i][j]!=b[i+1][j]){
				g[b[i][j]].push_back(b[i+1][j]);
				g[b[i+1][j]].push_back(b[i][j]);
			}
		}
	}
	for(int i=0; i<c; i++){
		sort(g[i].begin(), g[i].end());
		g[i].erase(unique(g[i].begin(), g[i].end()), g[i].end());
	}
	dfs3(0, -1);
	cout<<max(dp[0][0], dp[1][0])<<endl;
	return 0;
}
0