結果

問題 No.307 最近色塗る問題多くない?
ユーザー んんんんんん
提出日時 2023-01-05 23:38:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,585 bytes
コンパイル時間 2,785 ms
コンパイル使用メモリ 223,396 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-08-19 20:22:42
合計ジャッジ時間 12,764 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 12 ms
4,652 KB
testcase_08 AC 57 ms
7,008 KB
testcase_09 AC 25 ms
5,108 KB
testcase_10 AC 163 ms
4,380 KB
testcase_11 AC 1,104 ms
5,648 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 23 ms
4,380 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 5 ms
4,380 KB
testcase_17 AC 19 ms
4,816 KB
testcase_18 AC 32 ms
6,452 KB
testcase_19 AC 36 ms
6,752 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 30 ms
6,928 KB
testcase_22 AC 36 ms
7,384 KB
testcase_23 AC 29 ms
7,232 KB
testcase_24 AC 31 ms
7,028 KB
testcase_25 AC 54 ms
6,704 KB
testcase_26 AC 53 ms
7,024 KB
testcase_27 AC 617 ms
6,980 KB
testcase_28 TLE -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct UnionFind {
    vector<int> data;

    UnionFind(int sz) {
        data.assign(sz, -1);
    }

    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y) return false;
        data[y] = x;
        return true;
    }

    int find(int k) {
        if (data[k] < 0) return k;
        return data[k] = find(data[k]);
    }

    int color(int k) {
        return -data[find(k)]-1;
    }

    void change(int k, int a) {
        data[find(k)] = -a-1;
    }
};
int H, W;

int idx(int h, int w) {
    if (H > W) return h*H + w;
    return h + W*w;
}

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

int main() {
    cin >> H >> W;
    vector<vector<int>> A(H, vector<int>(W));
    for (auto &v : A) for (auto &e : v) cin >> e;

    UnionFind uf(max(H, W)*max(H, W));
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            int id = idx(i, j);
            uf.change(id, A[i][j]);
        }
    }

    vector<set<pair<int, int>>> vst(max(H, W)*max(H, W));
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            int id = idx(i, j);
            for (int d = 0; d < 4; d++) {
                int ni = i + dx[d];
                int nj = j + dy[d];
                if (ni < 0 || ni >= H || nj < 0 || nj >= W) continue;
                int nid = idx(ni, nj);
                if (uf.color(id) == uf.color(nid)) uf.unite(id, nid);
            }
        }
    }

    
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            bool f = false;
            int id = idx(i, j);
            for (int d = 0; d < 4; d++) {
                int ni = i + dx[d];
                int nj = j + dy[d];
                if (ni < 0 || ni >= H || nj < 0 || nj >= W) continue;
                int nid = idx(ni, nj);
                if (uf.find(id) != uf.find(nid)) {
                    f = true;
                }
            }
            vst[uf.find(id)].emplace(i, j);
        }
    }

    int Q;
    cin >> Q;
    while (Q--) {
        int r, c, x;
        cin >> r >> c >> x;
        r--; c--;
        int sid = idx(r, c);
        int p = uf.find(sid);
        set<pair<int, int>> st;
        for (auto &[i, j] : vst[p]) {
            int id = idx(i, j);
            for (int d = 0; d < 4; d++) {
                int ni = i + dx[d];
                int nj = j + dy[d];
                if (ni < 0 || ni >= H || nj < 0 || nj >= W) continue;
                int nid = idx(ni, nj);
                if (uf.find(id) != uf.find(nid) && uf.color(nid) == x) {
                    int np = uf.find(nid);
                    st.insert(vst[np].begin(), vst[np].end());
                    vst[np].clear();
                    uf.unite(p, nid);
                }
            }
        }
        uf.change(sid, x);
        vst[p].insert(st.begin(), st.end());
        vector<pair<int, int>> vp;
        for (auto &[i, j] : vst[p]) {
            bool f = true;
            int id = idx(i, j);
            for (int d = 0; d < 4; d++) {
                int ni = i + dx[d];
                int nj = j + dy[d];
                if (ni < 0 || ni >= H || nj < 0 || nj >= W) continue;
                int nid = idx(ni, nj);
                if (uf.find(id) != uf.find(nid)) f = false;
            }
            if (f) vp.emplace_back(make_pair(i, j));
        }

        for (auto pa : vp) vst[p].erase(pa);
    }

    for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) {
        int id = idx(i, j);
        cout << uf.color(id) << (j == W-1 ? "\n" : " ");
    }
}
0