結果

問題 No.307 最近色塗る問題多くない?
ユーザー minato
提出日時 2020-08-25 22:33:51
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
RE  
実行時間 -
コード長 3,550 bytes
コンパイル時間 4,290 ms
コンパイル使用メモリ 252,852 KB
最終ジャッジ日時 2025-01-13 14:51:07
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 13 WA * 1 RE * 21 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln = '\n';
//constexpr long long MOD = 1000000007;
constexpr long long MOD = 998244353;
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; }
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; }
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
void print() { cout << "\n"; }
template<class T, class... Args>
void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

struct UnionFind {
    int N;
    vector<int> node;

    UnionFind(){}
    UnionFind(int N) : N(N), node(N,-1) {}

    void init(int x) {
        node.assign(x,-1);
        N = x;
    }

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (node[x] > node[y]) swap(x,y);
        node[x] += node[y];
        node[y] = x;
        N--;
        return true; 
    }

    bool same(int x, int y) {return root(x) == root(y);}

    int root(int x) {
        if (node[x] < 0) return x;
        return node[x] = root(node[x]);
    }

    int size(int x) {return -node[root(x)];}

    int count() const {return N;}
};

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

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int H,W; cin >> H >> W;
    vector<vector<int>> A(H, vector<int>(W));
    rep(i,H) rep(j,W) cin >> A[i][j];

    UnionFind uf(H*W);
    vector<set<pii>> neighbor(H*W+1);
    vector<int> color(H*W);

    auto mer=[&](int r1, int r2) {
        if (uf.root(r1)!=r1) swap(r1,r2);
        if (neighbor[r1].size() < neighbor[r2].size()) swap(neighbor[r1],neighbor[r2]);
        neighbor[r1].merge(neighbor[r2]);
    };
    rep(i,H) rep(j,W) {
        color[i*W+j] = A[i][j];
        rep(k,4) {
            int nx = i + dx[k];
            int ny = j + dy[k];
            if (nx < 0 or nx >= H or ny < 0 or ny >= W) continue;
            if (A[i][j]==A[nx][ny]) {
                int r1 = uf.root(i*W+j);
                int r2 = uf.root(nx*W+ny);
                if (uf.merge(r1,r2)) mer(r1,r2);
            } else {
                neighbor[uf.root(i*W+j)].emplace(nx,ny);
            }
        }
    }

    int Q; cin >> Q;
    while (Q--) {
        int r, c, X; cin >> r >> c >> X;
        --r; --c;
        if (color[uf.root(r*W+c)]==X) continue;
        color[uf.root(r*W+c)] = X;
        set<pii> dic;
        for (auto [x,y] : neighbor[uf.root(r*W+c)]) {
            if (dic.size() < neighbor[x*W+y].size()) swap(dic,neighbor[x*W+y]);
            dic.merge(neighbor[x*W+y]);
        }
        int k = uf.root(r*W+c);
        for (auto [x,y] : neighbor[k]) {
            uf.merge(r*W+c,x*W+y);
        }
        if (neighbor[uf.root(r*W+c)].size() < dic.size()) swap(neighbor[uf.root(r*W+c)],dic);
        neighbor[uf.root(r*W+c)].merge(dic);
    }

    rep(i,H) {
        rep(j,W) cout << color[uf.root(i*W+j)] << " ";
        cout << ln;
    }

}
0