結果
| 問題 | 
                            No.307 最近色塗る問題多くない?
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2019-04-08 23:46:42 | 
| 言語 | D  (dmd 2.109.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 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 36 | 
ソースコード
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) {
  }
}