結果

問題 No.307 最近色塗る問題多くない?
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-04-08 23:46:42
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 86 ms / 4,000 ms
コード長 3,988 bytes
コンパイル時間 7,761 ms
コンパイル使用メモリ 662,684 KB
実行使用メモリ 13,396 KB
最終ジャッジ日時 2023-09-04 00:30:31
合計ジャッジ時間 10,335 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,500 KB
testcase_04 AC 4 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 15 ms
5,244 KB
testcase_08 AC 66 ms
13,296 KB
testcase_09 AC 33 ms
10,444 KB
testcase_10 AC 13 ms
5,304 KB
testcase_11 AC 30 ms
7,752 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 30 ms
5,260 KB
testcase_14 AC 6 ms
4,708 KB
testcase_15 AC 9 ms
4,996 KB
testcase_16 AC 7 ms
4,904 KB
testcase_17 AC 27 ms
7,100 KB
testcase_18 AC 46 ms
11,632 KB
testcase_19 AC 52 ms
11,184 KB
testcase_20 AC 5 ms
4,448 KB
testcase_21 AC 51 ms
11,332 KB
testcase_22 AC 51 ms
10,456 KB
testcase_23 AC 54 ms
12,088 KB
testcase_24 AC 53 ms
12,948 KB
testcase_25 AC 85 ms
13,396 KB
testcase_26 AC 84 ms
12,880 KB
testcase_27 AC 64 ms
9,156 KB
testcase_28 AC 81 ms
12,128 KB
testcase_29 AC 22 ms
5,140 KB
testcase_30 AC 86 ms
12,972 KB
testcase_31 AC 83 ms
10,452 KB
testcase_32 AC 75 ms
9,120 KB
testcase_33 AC 56 ms
10,108 KB
testcase_34 AC 84 ms
10,316 KB
testcase_35 AC 85 ms
11,300 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