結果

問題 No.307 最近色塗る問題多くない?
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-04-08 23:35:36
言語 D
(dmd 2.107.1)
結果
TLE  
実行時間 -
コード長 3,991 bytes
コンパイル時間 8,442 ms
コンパイル使用メモリ 667,844 KB
実行使用メモリ 9,020 KB
最終ジャッジ日時 2023-09-04 00:29:09
合計ジャッジ時間 17,584 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 5 ms
4,580 KB
testcase_05 AC 2 ms
4,356 KB
testcase_06 AC 2 ms
4,356 KB
testcase_07 AC 3,559 ms
9,020 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
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.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 (uf[u] > uf[v]) {
            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];
          int[] vs;
          foreach (v; graph[u]) {
            vs ~= root(v);
          }
          vs = vs.sort.uniq.array;
          foreach (v; vs) {
            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