結果

問題 No.307 最近色塗る問題多くない?
ユーザー te-shte-sh
提出日時 2017-06-19 14:30:55
言語 D
(dmd 2.106.1)
結果
MLE  
実行時間 -
コード長 1,968 bytes
コンパイル時間 755 ms
コンパイル使用メモリ 97,152 KB
実行使用メモリ 812,768 KB
最終ジャッジ日時 2023-09-03 14:31:51
合計ジャッジ時間 3,974 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 MLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
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.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto rd1 = readln.split.to!(size_t[]), h = rd1[0], w = rd1[1], hw = h * w;

  auto a = new int[](hw);
  foreach (i; 0..h) {
    auto rd2 = readln.split.to!(int[]);
    a[i*w..(i+1)*w][] = rd2[];
  }

  auto uf = UnionFind!size_t(hw);
  foreach (i; 0..hw) {
    if (i % w != w-1 && a[i] == a[i+1]) uf.unite(i, i+1);
    if (i / w != h-1 && a[i] == a[i+w]) uf.unite(i, i+w);
  }

  auto nn = new size_t[][](hw);
  foreach (i; 0..hw) {
    if (i % w != 0 && a[i] != a[i-1]) nn[uf.find(i)] ~= uf.find(i-1);
    if (i / w != 0 && a[i] != a[i-w]) nn[uf.find(i)] ~= uf.find(i-w);
    if (i % w != w-1 && a[i] != a[i+1]) nn[uf.find(i)] ~= uf.find(i+1);
    if (i / w != h-1 && a[i] != a[i+w]) nn[uf.find(i)] ~= uf.find(i+w);
  }

  foreach (i; 0..hw) {
    nn[i].sort();
    nn[i] = nn[i].uniq.array;
  }

  auto q = readln.chomp.to!size_t;
  foreach (_; 0..q) {
    auto rd3 = readln.split, r = rd3[0].to!size_t, c = rd3[1].to!size_t, x = rd3[2].to!int;
    auto i = uf.find((r-1) * w + (c-1));
    a[i] = x;

    foreach (j; nn[i]) {
      if (a[j] == x) {
        uf.unite(i, j);
        nn[i] ~= nn[j];
      }
    }
  }

  foreach (i; 0..h) {
    foreach (j; 0..w) {
      auto k = uf.find(i * w + j);
      write(a[k]);
      if (j < w-1) write(" ");
    }
    writeln();
  }
}

struct UnionFind(T)
{
  import std.algorithm, std.range;

  T[] p; // parent
  const T s; // sentinel
  const T n;

  this(T n)
  {
    this.n = n;
    p = new T[](n);
    s = n + 1;
    p[] = s;
  }

  T find(T i)
  {
    if (p[i] == s) {
      return i;
    } else {
      p[i] = find(p[i]);
      return p[i];
    }
  }

  void unite(T i, T j)
  {
    auto pi = find(i), pj = find(j);
    if (pi != pj) p[pj] = pi;
  }

  bool isSame(T i, T j) { return find(i) == find(j); }

  auto groups()
  {
    auto g = new T[][](n);
    foreach (i; 0..n) g[find(i)] ~= i;
    return g.filter!(l => !l.empty);
  }
}
0