結果
問題 | No.348 カゴメカゴメ |
ユーザー |
![]() |
提出日時 | 2016-05-19 22:02:33 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 75 ms / 2,000 ms |
コード長 | 2,944 bytes |
コンパイル時間 | 2,104 ms |
コンパイル使用メモリ | 191,096 KB |
実行使用メモリ | 17,704 KB |
最終ジャッジ日時 | 2024-10-11 01:03:14 |
合計ジャッジ時間 | 4,659 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 53 |
ソースコード
#include <bits/stdc++.h> using namespace std; struct node{ int sz; list<int> edge; node(){sz=1;}; }; bool check(int r,int c,int N,int M){ if(0<=r&&r<N)if(0<=c&&c<M)return true; return false; } int dir4[]={-1,0,1,0,-1}; int dir8[]={0,-1,-1,0,1,-1,1,1,0}; #define mp make_pair int main() { int N,M;cin>>N>>M;N+=2,M+=2; vector<string> grid(N); using VB=vector<bool>; using VVB=vector<VB>; VVB used(N,VB(M,false)); vector<node> tree(1);tree[0].sz=0; for(int i = 0; i < M; i++)grid[0]+='.',grid[N-1]+='.'; for(int i=1;i<N;i++){ cin>>grid[i]; grid[i]="."+grid[i]+"."; } int D=0; std::function<void(int,int,int)> dfs=[&](int r,int c,int now){ D++; used[r][c]=true; stack<pair<int,int>> W; vector<pair<int,int>> X; W.push(mp(r,c)); while(W.empty()==false){ r=W.top().first;c=W.top().second;W.pop(); for(int i = 0; i < 4; i++){ int nr=r+dir4[i],nc=c+dir4[i+1]; if(check(nr,nc,N,M)&&used[nr][nc]==false){ used[nr][nc]=true; if(grid[nr][nc]=='.')W.push(mp(nr,nc)); else X.push_back(mp(nr,nc)); } } } for(auto& it : X){ r=it.first,c=it.second; used[r][c]=false; } for(auto& it : X){ r=it.first,c=it.second; if(used[r][c]==false){ tree[now].edge.push_back(tree.size()); tree.push_back(node()); W.push(mp(r,c)); used[r][c]=true; while(W.empty()==false){ r=W.top().first;c=W.top().second;W.pop(); grid[r][c]='0'+D; for(int i = 0; i < 8; i++){ int nr=r+dir8[i],nc=c+dir8[i+1]; if(check(nr,nc,N,M)&&used[nr][nc]==false&&grid[nr][nc]=='x'){ tree.back().sz++; used[nr][nc]=true; W.push(mp(nr,nc)); } } } for(int i = 0; i < 4; i++){ int nr=r+dir4[i],nc=c+dir4[i+1]; if(check(nr,nc,N,M)&&used[nr][nc]==false){ dfs(nr,nc,tree.size()-1); } } } } D--; }; dfs(0,0,0); int Z=tree.size(); vector<pair<int,int>> dp(Z,mp(0,0)); std::function<void(int)> dfs2=[&](int v){ dp[v].first=tree[v].sz; dp[v].second=0; for(auto& u : tree[v].edge){ dfs2(u); dp[v].first+=dp[u].second; dp[v].second+=max(dp[u].first,dp[u].second); } }; //for(auto& it : grid){cout << it << endl;} dfs2(0); cout<<max(dp[0].first,dp[0].second)<<endl; return 0; }