結果

問題 No.2946 Puyo
ユーザー Hydrogen332
提出日時 2024-10-29 22:00:09
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 1,747 bytes
コンパイル時間 2,185 ms
コンパイル使用メモリ 201,624 KB
最終ジャッジ日時 2025-02-25 01:28:37
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)s; i < (int)e; ++i)
#define all(a) (a).begin(), (a).end()

struct UnionFind {
    vector<int> par, siz;
    int v, group;
    
    UnionFind(int n) {
        par = vector<int>(n, -1);
        siz = vector<int>(n, 1);
        v = n;
        group = n;
    }
    
    int root(int x) {
        if (par[x] == -1) return x;
        else return par[x] = root(par[x]);
    }
    
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    
    bool unite(int x, int y) {
        x = root(x);
        y = root(y);
        
        if (x == y) return false;
        
        if (siz[x] < siz[y]) swap(x, y);
        par[y] = x;
        siz[x] += siz[y];
        group--;
        return true;
    }
    
    int size(int x) {
        return siz[root(x)];
    }
};

int main() {
    cin.tie(nullptr);
    
    int H, W;
    cin >> H >> W;
    vector G(H, vector<char>(W));
    rep(i, 0, H) rep(j, 0, W) cin >> G[i][j];
    
    UnionFind uf(H * W);
    
    rep(i, 0, H) rep(j, 0, W) {
        if (i != 0) {
            if (G[i][j] == G[i - 1][j]) uf.unite(i * W + j, (i - 1) * W + j);
        }
        
        if (i != H - 1) {
            if (G[i][j] == G[i + 1][j]) uf.unite(i * W + j, (i + 1) * W + j);
        }
        
        if (j != 0) {
            if (G[i][j] == G[i][j - 1]) uf.unite(i * W + j, i * W + j - 1);
        }
        
        if (j != W - 1) {
            if (G[i][j] == G[i][j + 1]) uf.unite(i * W + j, i * W + j + 1);
        }
    }
    
    rep(i, 0, H) rep(j, 0, W) {
        if (uf.size(i * W + j) >= 4) cout << '.';
        else cout << G[i][j];
        
        if (j == W - 1) cout << '\n';
    }
}
0