結果

問題 No.307 最近色塗る問題多くない?
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-04-08 23:46:42
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 80 ms / 4,000 ms
コード長 3,988 bytes
コンパイル時間 8,750 ms
コンパイル使用メモリ 314,996 KB
実行使用メモリ 11,152 KB
最終ジャッジ日時 2024-06-13 04:59:58
合計ジャッジ時間 11,994 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 12 ms
5,376 KB
testcase_08 AC 61 ms
10,624 KB
testcase_09 AC 28 ms
7,168 KB
testcase_10 AC 11 ms
5,376 KB
testcase_11 AC 26 ms
6,272 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 25 ms
5,376 KB
testcase_14 AC 5 ms
5,376 KB
testcase_15 AC 7 ms
5,376 KB
testcase_16 AC 7 ms
5,376 KB
testcase_17 AC 29 ms
7,616 KB
testcase_18 AC 46 ms
11,152 KB
testcase_19 AC 47 ms
9,344 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 45 ms
8,704 KB
testcase_22 AC 46 ms
8,448 KB
testcase_23 AC 46 ms
9,984 KB
testcase_24 AC 47 ms
10,240 KB
testcase_25 AC 74 ms
10,240 KB
testcase_26 AC 80 ms
10,624 KB
testcase_27 AC 57 ms
8,192 KB
testcase_28 AC 69 ms
9,088 KB
testcase_29 AC 18 ms
5,376 KB
testcase_30 AC 74 ms
11,008 KB
testcase_31 AC 71 ms
8,448 KB
testcase_32 AC 67 ms
9,052 KB
testcase_33 AC 50 ms
9,472 KB
testcase_34 AC 74 ms
9,740 KB
testcase_35 AC 76 ms
9,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


int H, W;
int[][] A;
int Q;
int[] R, C, X;

void main() {
  try {
    for (; ; ) {
      H = readInt();
      W = readInt();
      A = new int[][](H, W);
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        A[x][y] = readInt();
      }
      Q = readInt();
      R = new int[Q];
      C = new int[Q];
      X = new int[Q];
      foreach (q; 0 .. Q) {
        R[q] = readInt() - 1;
        C[q] = readInt() - 1;
        X[q] = readInt();
      }
      
      auto uf = new int[H * W];
      auto colors = new int[H * W];
      auto graph = new int[][H * W];
      
      int root(int u) {
        return (uf[u] < 0) ? u : (uf[u] = root(uf[u]));
      }
      void connect(int u, int v) {
        debug {
          writeln("connect ", u, " ", v);
        }
        u = root(u);
        v = root(v);
        if (u != v) {
          if (graph[u].length < graph[v].length) {
            swap(u, v);
          }
          uf[u] += uf[v];
          uf[v] = u;
          graph[u] ~= graph[v];
          graph[v] = [];
        }
      }
      
      uf[] = -1;
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        const u = x * W + y;
        colors[u] = A[x][y];
      }
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        if (x > 0) {
          const u = (x - 1) * W + y, v = x * W + y;
          graph[u] ~= v;
          graph[v] ~= u;
        }
        if (y > 0) {
          const u = x * W + (y - 1), v = x * W + y;
          graph[u] ~= v;
          graph[v] ~= u;
        }
      }
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        if (x > 0 && A[x - 1][y] == A[x][y]) {
          const u = (x - 1) * W + y, v = x * W + y;
          connect(u, v);
        }
        if (y > 0 && A[x][y - 1] == A[x][y]) {
          const u = x * W + (y - 1), v = x * W + y;
          connect(u, v);
        }
      }
      debug {
        writeln("root = ");
        foreach (x; 0 .. H) {
          foreach (y; 0 .. W) {
            write(" ", root(x * W + y));
          }
          writeln();
        }
        writeln("colors = ");
        foreach (x; 0 .. H) {
          foreach (y; 0 .. W) {
            write(" ", colors[root(x * W + y)]);
          }
          writeln();
        }
        writeln("graph = ", graph);
      }
      
      foreach (q; 0 .. Q) {
        const u = root(R[q] * W + C[q]);
        if (colors[u] != X[q]) {
          colors[u] = X[q];
          const vs = graph[u].dup;
          graph[u] = [];
          foreach (v; vs) {
            assert(colors[root(v)] == X[q]);
            connect(u, v);
          }
        }
      }
      
      auto ans = new int[][](H, W);
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        ans[x][y] = colors[root(x * W + y)];
      }
      foreach (x; 0 .. H) {
        writeln(ans[x].to!string.replaceAll(regex(`[\[\],]`), ""));
      }
    }
  } catch (EOFException e) {
  }
}
0