結果

問題 No.307 最近色塗る問題多くない?
ユーザー t98slidert98slider
提出日時 2023-02-22 11:43:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,152 bytes
コンパイル時間 1,887 ms
コンパイル使用メモリ 183,328 KB
実行使用メモリ 34,824 KB
最終ジャッジ日時 2023-09-29 20:03:14
合計ジャッジ時間 10,269 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 7 ms
7,040 KB
testcase_08 AC 42 ms
27,160 KB
testcase_09 AC 19 ms
15,688 KB
testcase_10 AC 110 ms
7,188 KB
testcase_11 AC 770 ms
13,840 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 10 ms
4,380 KB
testcase_14 AC 4 ms
5,160 KB
testcase_15 AC 5 ms
6,448 KB
testcase_16 AC 4 ms
5,292 KB
testcase_17 AC 17 ms
14,332 KB
testcase_18 AC 28 ms
22,516 KB
testcase_19 AC 33 ms
27,052 KB
testcase_20 AC 3 ms
4,968 KB
testcase_21 AC 37 ms
29,324 KB
testcase_22 AC 38 ms
29,764 KB
testcase_23 AC 38 ms
28,400 KB
testcase_24 AC 36 ms
28,904 KB
testcase_25 AC 44 ms
27,964 KB
testcase_26 AC 48 ms
29,312 KB
testcase_27 AC 39 ms
30,192 KB
testcase_28 AC 49 ms
30,280 KB
testcase_29 AC 8 ms
4,376 KB
testcase_30 AC 50 ms
29,936 KB
testcase_31 AC 49 ms
30,316 KB
testcase_32 AC 50 ms
30,084 KB
testcase_33 AC 39 ms
30,508 KB
testcase_34 TLE -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int H, W;
    cin >> H >> W;
    vector<int> A(H * W);
    vector<deque<int>> B(H * W);
    vector<int> parent_or_size(H * W, -1);

    auto f = [&](int y, int x) { return y * W + x; };
    auto g = [&](int v) { return make_pair(v / W, v % W); };

    function<int(int)> leader = [&](int v){
        if(parent_or_size[v] < 0) return v;
        return parent_or_size[v] = leader(parent_or_size[v]);
    };

    auto merge = [&](int u, int v){
        int x = leader(u), y = leader(v);
        if(x == y)return;
        if(-parent_or_size[x] < -parent_or_size[y]) swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        B[x].insert(B[x].end(), B[y].begin(), B[y].end());
    };

    auto change = [&](int v, int c){
        int ld = leader(v);
        A[ld] = c;
        int n = B[ld].size();
        while(n--){
            int y, x, v;
            v = B[ld].front();
            B[ld].pop_front();
            tie(y, x) = g(v);
            int cnt = 0, cnt2 = 0;
            for(int i = 0; i < 4; i++){
                int ny = y + (i == 0) - (i == 1);
                int nx = x + (i == 2) - (i == 3);
                if(ny < 0 || nx < 0 || ny >= H || nx >= W) continue;
                cnt++;
                if(c == A[leader(f(ny, nx))]){
                    cnt2++;
                    merge(ld, f(ny, nx));
                }
            }
            if(cnt2 < cnt) B[ld].push_back(v);
        }
    };

    for(int y = 0; y < H; y++){
        for(int x = 0; x < W; x++){
            cin >> A[f(y, x)];
            B[f(y, x)].push_back(f(y, x));
            if(y && A[f(y, x)] == A[f(y - 1, x)]) merge(f(y, x), f(y - 1, x));
            if(x && A[f(y, x)] == A[f(y, x - 1)]) merge(f(y, x), f(y, x - 1));
        }
    }

    int Q, y, x, c;
    cin >> Q;
    while(Q--){
        cin >> y >> x >> c;
        change(f(--y, --x), c);
    }

    for(int y = 0; y < H; y++){
        for(int x = 0; x < W; x++){
            cout << A[leader(f(y, x))] << (x + 1 == W ? '\n' : ' ');
        }
    }
}
0