結果

問題 No.307 最近色塗る問題多くない?
ユーザー t98slidert98slider
提出日時 2023-02-22 11:44:43
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 51 ms / 4,000 ms
コード長 2,183 bytes
コンパイル時間 2,016 ms
コンパイル使用メモリ 184,504 KB
実行使用メモリ 30,420 KB
最終ジャッジ日時 2023-09-29 20:03:55
合計ジャッジ時間 4,469 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 8 ms
6,976 KB
testcase_08 AC 39 ms
26,972 KB
testcase_09 AC 21 ms
15,636 KB
testcase_10 AC 7 ms
7,452 KB
testcase_11 AC 18 ms
13,772 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 9 ms
4,376 KB
testcase_14 AC 4 ms
5,152 KB
testcase_15 AC 5 ms
6,144 KB
testcase_16 AC 4 ms
5,168 KB
testcase_17 AC 17 ms
14,456 KB
testcase_18 AC 32 ms
22,504 KB
testcase_19 AC 35 ms
27,036 KB
testcase_20 AC 4 ms
4,860 KB
testcase_21 AC 42 ms
29,472 KB
testcase_22 AC 39 ms
29,592 KB
testcase_23 AC 37 ms
28,336 KB
testcase_24 AC 39 ms
28,852 KB
testcase_25 AC 47 ms
28,240 KB
testcase_26 AC 50 ms
29,108 KB
testcase_27 AC 44 ms
30,280 KB
testcase_28 AC 50 ms
30,128 KB
testcase_29 AC 8 ms
4,376 KB
testcase_30 AC 51 ms
29,984 KB
testcase_31 AC 46 ms
30,148 KB
testcase_32 AC 43 ms
30,160 KB
testcase_33 AC 39 ms
30,420 KB
testcase_34 AC 45 ms
30,180 KB
testcase_35 AC 46 ms
30,364 KB
権限があれば一括ダウンロードができます

ソースコード

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);
        if(A[ld] == c) return;
        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