結果
問題 | No.348 カゴメカゴメ |
ユーザー | koyumeishi |
提出日時 | 2016-02-27 00:34:18 |
言語 | C++11 (gcc 11.4.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,531 bytes |
コンパイル時間 | 1,690 ms |
コンパイル使用メモリ | 134,600 KB |
実行使用メモリ | 61,020 KB |
最終ジャッジ日時 | 2024-09-24 10:46:28 |
合計ジャッジ時間 | 7,038 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 180 ms
21,748 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 514 ms
61,020 KB |
testcase_03 | AC | 338 ms
22,012 KB |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | WA | - |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | AC | 2 ms
6,944 KB |
testcase_11 | AC | 1 ms
6,944 KB |
testcase_12 | AC | 4 ms
6,944 KB |
testcase_13 | AC | 3 ms
6,944 KB |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | AC | 2 ms
6,944 KB |
testcase_17 | AC | 1 ms
6,944 KB |
testcase_18 | AC | 1 ms
6,940 KB |
testcase_19 | AC | 1 ms
6,944 KB |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | AC | 1 ms
6,940 KB |
testcase_23 | WA | - |
testcase_24 | AC | 216 ms
24,944 KB |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | AC | 204 ms
25,148 KB |
testcase_28 | AC | 206 ms
24,720 KB |
testcase_29 | WA | - |
testcase_30 | AC | 205 ms
24,528 KB |
testcase_31 | WA | - |
testcase_32 | AC | 205 ms
25,100 KB |
testcase_33 | AC | 17 ms
6,940 KB |
testcase_34 | AC | 16 ms
6,940 KB |
testcase_35 | AC | 16 ms
6,940 KB |
testcase_36 | AC | 17 ms
6,940 KB |
testcase_37 | AC | 16 ms
6,944 KB |
testcase_38 | AC | 16 ms
6,944 KB |
testcase_39 | AC | 17 ms
6,944 KB |
testcase_40 | AC | 16 ms
6,940 KB |
testcase_41 | AC | 17 ms
6,944 KB |
testcase_42 | AC | 16 ms
6,944 KB |
testcase_43 | AC | 4 ms
6,944 KB |
testcase_44 | AC | 4 ms
6,944 KB |
testcase_45 | AC | 4 ms
6,944 KB |
testcase_46 | WA | - |
testcase_47 | AC | 4 ms
6,940 KB |
testcase_48 | AC | 4 ms
6,940 KB |
testcase_49 | AC | 4 ms
6,940 KB |
testcase_50 | WA | - |
testcase_51 | AC | 4 ms
6,944 KB |
testcase_52 | AC | 4 ms
6,940 KB |
ソースコード
#include <iostream> #include <vector> #include <cstdio> #include <sstream> #include <map> #include <string> #include <algorithm> #include <queue> #include <cmath> #include <functional> #include <set> #include <ctime> #include <random> using namespace std; template<typename T> istream& operator >> (istream& is, vector<T>& vec){for(T& val: vec) is >> val; return is;} template<typename T> istream& operator , (istream& is, T& val){ return is >> val;} template<typename T> ostream& operator << (ostream& os, const vector<T>& vec){for(int i=0; i<vec.size(); i++) os << vec[i] << (i==vec.size()-1?"\n":" ");return os;} template<typename T> ostream& operator , (ostream& os, T& val){ return os << " " << val;} template<typename T> ostream& operator >> (ostream& os, T& val){ return os << " " << val;} using namespace std; class UnionFindTree{ struct base_node{ int parent; int rank; int size; }; vector<base_node> node; public: UnionFindTree(int n){ node.resize(n); for(int i=0; i<n; i++){ node[i].parent=i; node[i].rank=0; node[i].size=1; } } int find(int x){ //return root node of x if(node[x].parent == x) return x; else{ return node[x].parent = find(node[x].parent); } } bool same(int x, int y){ return find(x) == find(y); } int size(int at){ return node[find(at)].size; } void unite(int x, int y){ x = find(node[x].parent); y = find(node[y].parent); if(x==y) return; if(node[x].rank < node[y].rank){ node[x].parent = y; node[y].size += node[x].size; }else if(node[x].rank > node[y].rank){ node[y].parent = x; node[x].size += node[y].size; }else{ node[x].rank++; unite(x,y); } } }; int dx[] = {0,0,1,-1,1,-1,1,-1}; int dy[] = {1,-1,0,0,1,1,-1,-1}; int main(){ int n,m; cin >> n,m; vector<string> v(n+2, string(m+2, '.')); for(int i=1; i<=n; i++){ string tmp; cin >> tmp; v[i] = '.' + tmp + '.'; } n+=2; m+=2; UnionFindTree uft(n*m); for(int i=0; i<n; i++) for(int j=0; j<m; j++){ if(v[i][j] == 'x') for(int k=0; k<8; k++){ int ii = i+dy[k]; int jj = j+dx[k]; if(ii < 0 || ii>=n) continue; if(jj < 0 || jj>=m) continue; if(v[ii][jj] == 'x') uft.unite(i*m+j, ii*m+jj); } } int t=0; set<int> s; for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(v[i][j]=='x')s.insert(uft.find(i*m+j)); t=s.size(); //cerr << t << endl; map<int, set<int>> G; queue<int> q; vector<bool> visit( n*m , false); set<int> next; vector<vector<int>> w(n, vector<int>(m, -1)); {//round q.push(0); visit[0] = true; while(q.size()){ auto pos = q.front(); q.pop(); int r = pos/m; int c = pos%m; w[r][c] = 0; for(int k=0; k<4; k++){ int rr = r+dy[k]; int cc = c+dx[k]; if(rr < 0 || rr>=n) continue; if(cc < 0 || cc>=m) continue; if(visit[rr*m + cc]) continue; if(v[rr][cc] == 'x'){ next.insert(uft.find(rr*m + cc)); continue; } visit[rr*m + cc] = true; q.push(rr*m + cc); } } for(int unko : next){ q.push(unko); G[0].insert(unko); } } while(q.size()){ auto p = q.front(); q.pop(); int r = p/m; int c = p%m; int val = uft.find(p); set<int> tmp; { function<void(int,int)> dfs0 = [&](int r, int c){ w[r][c] = val; for(int k=0; k<8; k++){ int rr = r+dy[k]; int cc = c+dx[k]; if(rr < 0 || rr>=n) continue; if(cc < 0 || cc>=m) continue; if(w[rr][cc] != -1) continue; if(v[rr][cc] == '.'){ tmp.insert(uft.find(rr*m + cc)); continue; } dfs0(rr,cc); } }; dfs0(r,c); } set<int> next; function<void(int,int)> dfs = [&](int r, int c){ if(visit[r*m+c]) return; if(v[r][c] == 'x'){ next.insert(r*m + c); return; } w[r][c] = val; visit[r*m+c] = true; for(int k=0; k<4; k++){ int rr = r+dy[k]; int cc = c+dx[k]; if(rr < 0 || rr>=n) continue; if(cc < 0 || cc>=m) continue; if(w[rr][cc] != -1) continue; dfs(rr,cc); } }; for(int hoge : tmp) dfs(hoge/m, hoge%m); for(int ch : next){ G[p].insert(uft.find(ch)); q.push(ch); } } vector<map<int,long long>> memo(2); long long ans = 0; function<long long(int,int)> dfs = [&](int pos, int use){ if(memo[use].find(pos) != memo[use].end()) return memo[use][pos]; long long ret = 0; if(pos != -1 && use) ret += uft.size(pos); for(auto ch : G[pos]){ if(use) ret += dfs(ch, 0); else ret += max(dfs(ch, 0), dfs(ch, 1)); } if(pos != -1) memo[use][pos] = ret; return ret; }; ans = max(dfs(0,0), dfs(0,1)); cout << ans << endl; return 0; }