結果

問題 No.307 最近色塗る問題多くない?
ユーザー nebukuro09nebukuro09
提出日時 2017-05-16 13:57:10
言語 D
(dmd 2.105.2)
結果
TLE  
実行時間 -
コード長 3,038 bytes
コンパイル時間 1,090 ms
コンパイル使用メモリ 114,944 KB
実行使用メモリ 18,808 KB
最終ジャッジ日時 2023-09-03 13:25:35
合計ジャッジ時間 13,908 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,760 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 11 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 108 ms
12,384 KB
testcase_08 AC 2,749 ms
14,944 KB
testcase_09 AC 1,047 ms
14,968 KB
testcase_10 AC 100 ms
6,476 KB
testcase_11 AC 548 ms
14,104 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 37 ms
8,676 KB
testcase_14 AC 38 ms
6,896 KB
testcase_15 AC 78 ms
6,224 KB
testcase_16 AC 68 ms
7,436 KB
testcase_17 AC 1,840 ms
12,300 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

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