結果

問題 No.307 最近色塗る問題多くない?
ユーザー nebukuro09nebukuro09
提出日時 2017-05-16 14:16:14
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 604 ms / 4,000 ms
コード長 3,038 bytes
コンパイル時間 1,294 ms
コンパイル使用メモリ 115,092 KB
実行使用メモリ 16,588 KB
最終ジャッジ日時 2023-09-03 13:25:53
合計ジャッジ時間 6,252 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 11 ms
4,380 KB
testcase_08 AC 58 ms
9,060 KB
testcase_09 AC 22 ms
4,412 KB
testcase_10 AC 8 ms
4,380 KB
testcase_11 AC 17 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 33 ms
9,496 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 35 ms
4,380 KB
testcase_18 AC 100 ms
5,752 KB
testcase_19 AC 89 ms
7,000 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 68 ms
7,200 KB
testcase_22 AC 58 ms
5,452 KB
testcase_23 AC 525 ms
9,040 KB
testcase_24 AC 204 ms
8,004 KB
testcase_25 AC 294 ms
13,728 KB
testcase_26 AC 577 ms
13,200 KB
testcase_27 AC 46 ms
9,244 KB
testcase_28 AC 77 ms
14,000 KB
testcase_29 AC 27 ms
4,384 KB
testcase_30 AC 604 ms
13,184 KB
testcase_31 AC 65 ms
13,668 KB
testcase_32 AC 52 ms
14,036 KB
testcase_33 AC 23 ms
6,808 KB
testcase_34 AC 56 ms
15,904 KB
testcase_35 AC 62 ms
16,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;


void main() {
    auto s = readln.split.map!(to!int).array;
    auto H = s[0];
    auto W = s[1];
    auto A = H.iota.map!(_ => readln.split.map!(to!int).array).array;
    auto Q = readln.chomp.to!int;
    auto RCX = Q.iota.map!(_ => readln.split.map!(to!int).array).array;

    auto uf = new UnionFind(H*W);
    foreach (i; 0..H) foreach (j; 0..W) uf.color[i*W+j] = A[i][j];

    auto used = new bool[][](H, W);
    int[4] dr = [0, 0, -1, 1];
    int[4] dc = [-1, 1, 0, 0];

    void dfs(int r, int c, int p) {
        if (used[r][c]) return;
        used[r][c] = true;
        uf.unite(p, r*W+c, A[r][c]);
        foreach (i; 0..4) {
            int nr = r + dr[i];
            int nc = c + dc[i];
            if (nr < 0 || nr >= H || nc < 0 || nc >= W) continue;
            if (used[nr][nc]) continue;
            if (A[nr][nc] != A[r][c]) continue;
            dfs(nr, nc, p);
        }
    }

    foreach (i; 0..H)
        foreach (j; 0..W)
            if (!used[i][j])
                dfs(i, j, i*W+j);


    bool cancel = false;
    
    foreach (q; 0..Q) {
        RCX[q][0]--, RCX[q][1]--;
        auto n = uf.find(RCX[q][0] * W + RCX[q][1]);
        auto x = RCX[q][2];
        if (uf.color[n] == x) continue;
        if (uf.table[n] == -H*W) {
            cancel = true;
            break;
        }

        int[] to_unite;
        foreach (m; uf.members[n]) {
            int i = m / W;
            int j = m % W;
            foreach (k; 0..4) {
                int ni = i + dr[k];
                int nj = j + dc[k];
                if (ni < 0 || ni >= H || nj < 0 || nj >= W) continue;
                if (uf.find(m) == uf.find(ni*W+nj)) continue;
                to_unite ~= (ni * W + nj);
            }
        }

        foreach (tu; to_unite) uf.unite(n, tu, x);
    }

    if (cancel) uf.color[uf.find(0)] = RCX[$-1][2];


    auto ans = new int[][](H, W);

    foreach (n; 0..H*W) {
        if (uf.table[n] >= 0) continue;
        foreach (m; uf.members[n]) {
            int r = m / W;
            int c = m % W;
            ans[r][c] = uf.color[n];
        }
    }

    ans.each!(an => an.map!(a => a.to!string).join(" ").writeln);
}

class UnionFind {
    int N;
    int[] table;
    int[] color;
    int[][] members;

    this(int n) {
        N = n;
        table = new int[](N);
        color = new int[](N);
        members = new int[][](N);
        
        fill(table, -1);
        foreach (i; 0..n) members[i] = [i];
    }

    int find(int x) {
        return table[x] < 0 ? x : (table[x] = find(table[x]));
    }

    void unite(int x, int y, int c) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (table[x] > table[y]) swap(x, y);
        table[x] += table[y];
        table[y] = x;
        foreach (m; members[y]) members[x] ~= m;
        color[x] = c;
        members[y] = [];
    }
}
0