結果

問題 No.348 カゴメカゴメ
ユーザー snteasntea
提出日時 2016-10-19 15:43:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 156 ms / 2,000 ms
コード長 7,957 bytes
コンパイル時間 3,213 ms
コンパイル使用メモリ 220,072 KB
実行使用メモリ 22,880 KB
最終ジャッジ日時 2024-05-02 05:36:08
合計ジャッジ時間 7,201 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
外部呼び出し有り
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
10,624 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 156 ms
22,880 KB
testcase_03 AC 112 ms
12,288 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 49 ms
7,424 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 92 ms
9,472 KB
testcase_24 AC 91 ms
9,216 KB
testcase_25 AC 93 ms
9,308 KB
testcase_26 AC 92 ms
9,320 KB
testcase_27 AC 92 ms
9,328 KB
testcase_28 AC 94 ms
9,344 KB
testcase_29 AC 92 ms
9,180 KB
testcase_30 AC 94 ms
9,340 KB
testcase_31 AC 93 ms
9,036 KB
testcase_32 AC 94 ms
9,344 KB
testcase_33 AC 10 ms
5,376 KB
testcase_34 AC 10 ms
5,376 KB
testcase_35 AC 10 ms
5,376 KB
testcase_36 AC 11 ms
5,376 KB
testcase_37 AC 10 ms
5,376 KB
testcase_38 AC 10 ms
5,376 KB
testcase_39 AC 10 ms
5,376 KB
testcase_40 AC 10 ms
5,376 KB
testcase_41 AC 11 ms
5,376 KB
testcase_42 AC 11 ms
5,376 KB
testcase_43 AC 4 ms
5,376 KB
testcase_44 AC 4 ms
5,376 KB
testcase_45 AC 4 ms
5,376 KB
testcase_46 AC 4 ms
5,376 KB
testcase_47 AC 4 ms
5,376 KB
testcase_48 AC 4 ms
5,376 KB
testcase_49 AC 4 ms
5,376 KB
testcase_50 AC 4 ms
5,376 KB
testcase_51 AC 4 ms
5,376 KB
testcase_52 AC 5 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef LOCAL111
#else
	#define NDEBUG
#endif
#include <bits/stdc++.h>
const int INF = 1e9;
using namespace std;
template<typename T, typename U> ostream& operator<< (ostream& os, const pair<T,U>& p) { cout << '(' << p.first << ' ' << p.second << ')'; return os; }

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define SZ(a) int((a).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define RBP(i,a) for(auto& i : a)
#ifdef LOCAL111
	#define DEBUG(x) cout<<#x<<": "<<(x)<<endl
	template<typename T> void dpite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}
#else
	#define DEBUG(x) true
	template<typename T> void dpite(T a, T b){ return; }
#endif
#define F first
#define S second
#define SNP string::npos
#define WRC(hoge) cout << "Case #" << (hoge)+1 << ": "
#define rangej(a,b,c) ((a) <= (c) and (c) < (b))
#define rrangej(b,c) rangej(0,b,c)
template<typename T> void pite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}
template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}
template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}

typedef long long int LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
typedef pair<LL,LL> LP;

//library
typedef long long cost_t;

class Edge {
public:
	int to;
	cost_t cost;
	
	Edge(){
	}

	Edge(int x,cost_t y){
		to = x;
		cost = y;
	}

	bool operator< (const Edge& x) const {
		if(cost != x.cost) return cost < x.cost;
		return to < x.to;
	}

	bool operator> (const Edge& x) const {
		if(cost != x.cost) return cost > x.cost;
		return to > x.to;
	}
};

class Graph {
private:
	//const long long int INF = (long long)1e18;
	vector<vector<Edge> > v;
public:
	Graph(int x){
		v = vector<vector<Edge> >(x);
	}

	Graph(){

	}

	vector<Edge>& operator[](int x){
		return v[x];
	}

	const vector<Edge>& operator[](int x) const {
		return v[x];
	}

	int size() const {
		return v.size();
	}

	void add_edge(int from, Edge e){
		if(from >= size()){
			v.resize(from+1);
		}
		if(e.to >= size()){
			v.resize(e.to+1);
		}
		v[from].push_back(e);
	}

	void add_edge(int from, int to, cost_t cost){
		add_edge(from,Edge(to,cost));
	}

	void add_uedge(int from, int to, cost_t cost){
		add_edge(from,to,cost);
		add_edge(to,from,cost);
	}
};

vector<cost_t> dijkstra(int from, const Graph& v)
{
	vector<cost_t> dist(v.size(),INF);
	priority_queue<Edge,vector<Edge>,greater<Edge>> que;
	que.push(Edge(from,0));
	while(!que.empty()){
		Edge e = que.top();
		que.pop();
		if(dist[e.to] == INF){
			dist[e.to] = e.cost;
			for(auto to : v[e.to]){
				if(dist[to.to] == INF)
					que.push(Edge(to.to, e.cost+to.cost));
			}
		}
	}
	return dist;
}

vector<cost_t> bellmanford(int from, const Graph& g)
{
	vector<cost_t> res(g.size(),INF);
	res[from] = 0;
	bool conf = true;
	int cnt = 0;
	while(conf){
		conf = false;
		for(int i = 0; i < g.size(); i++){
			if(res[i] != INF)
			for(const auto& e : g[i]){
				if(res[e.to] > res[i]+e.cost){
					conf = true;
					res[e.to] = res[i]+e.cost;
				}
			}
		}
		if(cnt > g.size()+5) return vector<cost_t>();
		cnt++;
	}
	return res;
}

Graph prim(const Graph g)
{
	using T = tuple<cost_t, int, int>;
	priority_queue<T, vector<T>, greater<T>> qu;
	int n = g.size();
	assert(n != 0);
	vector<bool> added(n,false);
	qu.emplace(0,0,0);
	Graph res(n);
	while(!qu.empty()){
		int from, to;
		cost_t cost;
		tie(cost, from, to) = qu.top();
		qu.pop();
		if(!added[to]){
			added[to] = true;
			res.add_edge(from, to, cost);
			res.add_edge(to, from, cost);
			for(const auto &e : g[to]){
				if(!added[e.to]){
					qu.emplace(e.cost, to, e.to);
				}
			}
		}
	}
	return res;
}

vector<int> tree_getorder(const Graph &g, int root)
{
	vector<int> res;
	function<void(int p, int pre)> func = [&](int p, int pre){
		for(auto &e : g[p]){
			if(e.to != pre){
				func(e.to,p);
			}
		}
		res.push_back(p);
	};
	func(root,-1);
	return res;
}

vector<int> tree_getpar(const Graph &g, int root)
{
	vector<int> par(g.size(),root);
	function<void(int p, int pre)> func = [&](int p, int pre){
		par[p] = pre;
		for(auto &e : g[p]){
			if(e.to != pre){
				func(e.to,p);
			}
		}
	};
	func(root,root);
	return par;
}


//未検証
vector<int> toposort(const Graph &g, int start)
{
	int n = g.size();
	vector<int> res;
	res.reserve(n);
	vector<bool> used(n,false);
	function<void(int)> rec = [&](int p){
		if(used[p]) return;
		used[p] = true;
		for(auto&& e : g[p]) {
			rec(e.to);
		}
		res.push_back(p);
	};
	rec(start);
	return res;
}

//liblary

void vizGraph(const Graph &g, bool with_cap = false){
	ofstream ofs("./out.dot");
	ofs << "digraph graph_name {" << endl;
	for(int i = 0; i < (int)g.size(); i++){
		if (!g[i].size())
			continue;
		for(const auto &e : g[i]){
			ofs << "    \"" << i << "\" -> \"" << e.to << '"'; 
			if (with_cap) {
				ofs << " [ label = \"" << (e.cost  == INF ? "inf" : to_string(e.cost)) << "\"];"; 
			}
			ofs << endl;
		}
	}
	ofs << "}" << endl;
	ofs.close();
	system("dot -T png out.dot > sample.png");  
}

void ios_init(){
	//cout.setf(ios::fixed);
	//cout.precision(12);
#ifdef LOCAL111
	return;
#endif
	ios::sync_with_stdio(false); cin.tie(0);    
}

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



int main()
{
	ios_init();
	int n,m;
	while(cin >> n >> m) {
		vector<string> grid(n);
		REP(i,n) cin >> grid[i];
		vector<string> gr(n+4,string(m+4,'x'));
		gr[1] = gr[n+2] = 'x'+string(m+2,'.')+'x';
		REP(i,n){
			gr[i+2] = "x."+grid[i]+".x";
		}
		Graph g;
		vector<vector<bool>> used(n+4,vector<bool>(m+4,false));
		REP(i,n+4){
			DEBUG(gr[i]);
		}
		vector<int> costv;
		
		function<int(int,int)> dfs = [&](int x, int y) -> int{
			function<bool(int,int)> judge = [&](int x, int y)->bool{
				return (0 <= x and x < SZ(gr) and 0 <= y and y < SZ(gr[x]));
			};
			REP(i,SZ(used)) dpite(ALL(used[i]));
			if(used[x][y]) return -1;
			int self = costv.size();
			// DEBUG(x); DEBUG(y);
			DEBUG(self);
			// queue<P> qu;
			queue<P> quu;
			// qu.emplace(x, y);
			int cnt = 0;
			function<bool(int,int,int,int)> dfss
			= [&](int x, int y, int px, int py) -> bool{
				if(gr[x][y] == '.' and used[x][y]) return true;
				DEBUG(x); DEBUG(y);
				DEBUG(gr[x][y]); DEBUG(used[x][y]);
				bool res = true;
				if(gr[x][y] == 'x'){
					if(used[x][y]){
						DEBUG(222222222222222);
						DEBUG(px); DEBUG(py);
						return false;
					}
					used[x][y] = true;
					cnt++;
					FOR(dx,-1,2) FOR(dy,-1,2){
						if(dx == 0 and dy == 0) continue;
						DEBUG(x+dx); DEBUG(y+dy); DEBUG(px); DEBUG(py);
						if(judge(x+dx,y+dy) and (px != x+dx or py != y+dy)){
							res &= dfss(x+dx,y+dy,x,y);
						}
					}
				}else{
					if(!used[x][y]) quu.emplace(x,y);
					else return true;
				}
				return res;
			};
			bool ff = dfss(x,y,-1,-1);
			if(ff) return -1;
			costv.push_back(cnt);
			stack<P> sta;
			while(!quu.empty()) {
				int x,y;
				tie(x, y) = quu.front();
				// DEBUG(x); DEBUG(y);
				quu.pop();
				if(!used[x][y]) {
					if(gr[x][y] == '.') {
						used[x][y] = true;
						REP(i,4) if(judge(x+dx[i], y+dy[i])){
							quu.emplace(x+dx[i], y+dy[i]);
						}
					} else {
						sta.emplace(x,y);
					}
				}
			}
			while(!sta.empty()){
				int x, y;
				tie(x, y) = sta.top();
				sta.pop();
				auto res = dfs(x, y);
				if(res != -1){
					g.add_uedge(self,res,-1);
				}
			}
			return self;
		};
		dfs(0,0);
		REP(i,SZ(used)){
			dpite(ALL(used[i]));
		}
		vizGraph(g);
		if(SZ(g) == 0){
			cout << 0 << endl;
			continue;
		}
		auto topo = tree_getorder(g,0);
		vector<vector<int>> dp(SZ(g),vector<int>(2,0));
		for(auto&& e : topo) {
			dp[e][1] = costv[e];
			for(auto&& ee : g[e]) {
				dp[e][0] += max(dp[ee.to][0], dp[ee.to][1]);
				dp[e][1] += dp[ee.to][0];
			}
		}
		cout << dp[0][0] << endl;
	}
	return 0;
}
0