結果

問題 No.348 カゴメカゴメ
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-23 22:44:38
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 1,309 ms / 2,000 ms
コード長 4,715 bytes
コンパイル時間 28,296 ms
コンパイル使用メモリ 120,392 KB
実行使用メモリ 178,972 KB
最終ジャッジ日時 2023-09-03 22:58:41
合計ジャッジ時間 49,188 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,067 ms
115,544 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 1,309 ms
178,972 KB
testcase_03 AC 1,309 ms
87,860 KB
testcase_04 AC 4 ms
4,388 KB
testcase_05 AC 5 ms
4,508 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 30 ms
11,012 KB
testcase_13 AC 11 ms
5,532 KB
testcase_14 AC 3 ms
4,384 KB
testcase_15 AC 1,026 ms
176,180 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,384 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1,141 ms
164,500 KB
testcase_24 AC 1,141 ms
145,184 KB
testcase_25 AC 1,143 ms
145,676 KB
testcase_26 AC 1,144 ms
144,384 KB
testcase_27 AC 1,120 ms
144,900 KB
testcase_28 AC 1,136 ms
164,292 KB
testcase_29 AC 1,128 ms
144,888 KB
testcase_30 AC 1,161 ms
165,888 KB
testcase_31 AC 1,138 ms
144,380 KB
testcase_32 AC 1,134 ms
164,644 KB
testcase_33 AC 88 ms
21,524 KB
testcase_34 AC 82 ms
21,228 KB
testcase_35 AC 86 ms
23,056 KB
testcase_36 AC 85 ms
23,204 KB
testcase_37 AC 85 ms
21,684 KB
testcase_38 AC 84 ms
23,552 KB
testcase_39 AC 83 ms
21,824 KB
testcase_40 AC 84 ms
21,404 KB
testcase_41 AC 85 ms
21,664 KB
testcase_42 AC 85 ms
23,252 KB
testcase_43 AC 13 ms
5,512 KB
testcase_44 AC 13 ms
5,456 KB
testcase_45 AC 13 ms
6,788 KB
testcase_46 AC 13 ms
5,480 KB
testcase_47 AC 12 ms
5,476 KB
testcase_48 AC 13 ms
7,036 KB
testcase_49 AC 13 ms
5,524 KB
testcase_50 AC 13 ms
6,512 KB
testcase_51 AC 13 ms
6,508 KB
testcase_52 AC 13 ms
5,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, 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(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }


int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}


int M, N;
string[] A;

int[][] G;
int[] scores;

// (discard, use)
Tuple!(int, int) dfs(int u) {
  auto ret = tuple(0, scores[u]);
  foreach (v; G[u]) {
    auto res = dfs(v);
    ret[0] += max(res[0], res[1]);
    ret[1] += res[0];
  }
  return ret;
}

void main() {
  try {
    for (; ; ) {
      M = readInt();
      N = readInt();
      A = new string[M + 2];
      foreach (x; [0, M + 1]) {
        foreach (y; 0 .. N + 2) {
          A[x] ~= ".";
        }
      }
      foreach (x; 1 .. M + 1) {
        A[x] = "." ~ readToken() ~ ".";
      }
      M += 2;
      N += 2;
      
      auto uf = new int[M * N];
      uf[] = -1;
      foreach (x; 0 .. M) foreach (y; 0 .. N) {
        if (A[x][y] == 'x') {
          foreach (dir; 0 .. 4) {
            const xx = x + [+1, +1, 0, -1, -1, -1, 0, +1][dir];
            const yy = y + [0, +1, +1, +1, 0, -1, -1, -1][dir];
            if (0 <= xx && xx < M && 0 <= yy && yy < N && A[xx][yy] == 'x') {
              uf.connect(x * N + y, xx * N + yy);
            }
          }
        }
      }
      int V = 1;
      auto ids = new int[][](M, N);
      foreach (x; 0 .. M) foreach (y; 0 .. N) {
        ids[x][y] = -1;
      }
      foreach (x; 0 .. M) foreach (y; 0 .. N) {
        if (A[x][y] == 'x' && uf[x * N + y] < 0) {
          ids[x][y] = V++;
        }
      }
      foreach (x; 0 .. M) foreach (y; 0 .. N) {
        if (A[x][y] == 'x') {
          const r = uf.root(x * N + y);
          ids[x][y] = ids[r / N][r % N];
        }
      }
      debug {
        writeln("ids");
        foreach (x; 0 .. M) {
          writeln(ids[x]);
        }
      }
      
      // 0-1 BFS from outside
      // (depth, last ring)
      auto q = DList!(Tuple!(int, int))();
      auto d = new Tuple!(int, int)[][](M, N);
      auto vis = new bool[][](M, N);
      foreach (x; 0 .. M) foreach (y; 0 .. N) {
        d[x][y] = tuple(M * N, -1);
        vis[x][y] = false;
      }
      d[0][0] = tuple(0, 0);
      q.insertBack(tuple(0, 0));
      for (; !q.empty; ) {
        const x = q.front[0];
        const y = q.front[1];
        q.removeFront;
        if (!vis[x][y]) {
          vis[x][y] = true;
          const cc = (A[x][y] == 'x') ? tuple(d[x][y][0] + 1, ids[x][y]) : d[x][y];
          foreach (dir; 0 .. 4) {
            int xx = x + [+1, 0, -1, 0][dir];
            int yy = y + [0, +1, 0, -1][dir];
            if (0 <= xx && xx < M && 0 <= yy && yy < N) {
              if (d[xx][yy] > cc) {
                d[xx][yy] = cc;
                (A[x][y] == 'x') ? q.insertBack(tuple(xx, yy)) : q.insertFront(tuple(xx, yy));
              }
            }
          }
        }
      }
      debug {
        writeln("d[][][0]");
        foreach (x; 0 .. M) {
          writeln(d[x].map!"a[0]");
        }
        writeln("d[][][1]");
        foreach (x; 0 .. M) {
          writeln(d[x].map!"a[1]");
        }
      }
      
      G = new int[][V];
      scores = new int[V];
      foreach (x; 0 .. M) foreach (y; 0 .. N) {
        if (A[x][y] == 'x' && uf[x * N + y] < 0) {
          G[d[x][y][1]] ~= ids[x][y];
          scores[ids[x][y]] = -uf[x * N + y];
        }
      }
      debug {
        writeln("G = ", G);
        writeln("scores = ", scores);
      }
      auto res = dfs(0);
      writeln(res[0]);
    }
  } catch (EOFException e) {
  }
}
0