Instantly share code, notes, and snippets.

# ctylim/yukicoder348.cpp Created Feb 27, 2016

What would you like to do?
yukicoder No.348
 #include #include #include #include #include #include #include #include #include #include #include #include #include #define repd(i,a,b) for (int i=(int)(a);i<(int)(b);i++) #define rep(i,n) repd(i,0,n) #define all(x) (x).begin(),(x).end() #define mod 1000000007 #define inf 2000000007 #define mp make_pair #define pb push_back typedef long long ll; using namespace std; template inline void output(T a, int p) { if(p) cout << fixed << setprecision(p) << a << "\n"; else cout << a << "\n"; } // end of template // union find class union_find { public: int n; vector parent, rnk, num; union_find(int n) : n(n), parent(n), rnk(n, 0), num(n, 1) {rep(i, n) parent[i] = i; } int root(int x){ return (parent[x] == x) ? x : root(parent[x]); } void unite(int x, int y){ x = root(x); y = root(y); if (x == y) { return; } if (rnk[x] < rnk[y]) { parent[x] = y; num[y] += num[x]; } else{ parent[y] = x; num[x] += num[y]; if (rnk[x] == rnk[y]) { rnk[x]++; } } n--; } bool same(int x, int y) {return root(x) == root(y);} int count(int x){ return num[root(x)]; } }; int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; int H, W; vector vis; vector> G, T, dp; vector S; void dfs(int pos, int ring, union_find &uf){ vis[pos] = 1; for(int c: G[pos]){ int cx = c % W, cy = c / W; rep(k, ring ? 8 : 4){ if (cx + dx[k] >= 0 && cx + dx[k] < W && cy + dy[k] >= 0 && cy + dy[k] < H) { if (S[cy + dy[k]][cx + dx[k]] == ring ? '.' : 'x' && vis[uf.root((cy + dy[k]) * W + cx + dx[k])] == 0) { T[pos].pb(uf.root((cy + dy[k]) * W + cx + dx[k])); dfs(uf.root((cy + dy[k]) * W + cx + dx[k]), ring ^ 1, uf); } } } } } void dfs_m(int now, union_find &uf){ // 一番浅いところにあるxの輪すべてについてこの操作を行う メモ化再帰 dp[now][1] = uf.count(now); for(int ch: T[now]) for(int chch : T[ch]){ dfs_m(chch, uf); dp[now][0] += max(dp[chch][0], dp[chch][1]); dp[now][1] += dp[chch][0]; } } int main() { cin.tie(0); ios::sync_with_stdio(0); // source code cin >> H >> W; H += 2, W += 2; S.resize(H); S[0] = S[H - 1] = string(W, '.'); rep(i, H - 2){ cin >> S[i + 1]; S[i + 1] = '.' + S[i + 1] + '.'; } union_find uf(H * W); rep(y, H) rep(x, W){ rep(k, S[y][x] == 'x' ? 8 : 4){ if (!(x + dx[k] >= 0 && x + dx[k] < W && y + dy[k] >= 0 && y + dy[k] < H)) continue; if (S[y + dy[k]][x + dx[k]] == S[y][x]){ uf.unite(y * W + x, (y + dy[k]) * W + x + dx[k]); } } } vis.assign(H * W, 0), G.resize(H * W), T.resize(H * W), dp.resize(H * W, vector(2)); rep(i, H * W) G[uf.root(i)].pb(i); dfs(uf.root(0), 0, uf); int ret = 0; for (int x: T[uf.root(0)]) { dfs_m(x, uf); ret += max(dp[x][0], dp[x][1]); } output(ret, 0); return 0; }