結果

問題 No.241 出席番号(1)
ユーザー te-shte-sh
提出日時 2016-09-21 14:05:39
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 2,686 bytes
コンパイル時間 860 ms
コンパイル使用メモリ 111,840 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-09-02 22:18:24
合計ジャッジ時間 3,571 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 1 ms
4,368 KB
testcase_04 AC 2 ms
4,368 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 1 ms
4,368 KB
testcase_07 AC 1 ms
4,372 KB
testcase_08 AC 1 ms
4,368 KB
testcase_09 AC 1 ms
4,368 KB
testcase_10 AC 1 ms
4,368 KB
testcase_11 AC 2 ms
4,372 KB
testcase_12 AC 2 ms
4,372 KB
testcase_13 AC 1 ms
4,372 KB
testcase_14 AC 3 ms
4,368 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 1 ms
4,368 KB
testcase_17 AC 1 ms
4,372 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 1 ms
4,372 KB
testcase_20 AC 2 ms
4,372 KB
testcase_21 AC 1 ms
4,368 KB
testcase_22 AC 1 ms
4,368 KB
testcase_23 AC 2 ms
4,368 KB
testcase_24 AC 4 ms
4,368 KB
testcase_25 AC 4 ms
4,372 KB
testcase_26 AC 4 ms
4,372 KB
testcase_27 AC 4 ms
4,372 KB
testcase_28 AC 4 ms
4,368 KB
testcase_29 AC 4 ms
4,372 KB
testcase_30 AC 3 ms
4,368 KB
testcase_31 AC 4 ms
4,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.regex, std.conv, std.stdio, std.typecons;

void main()
{
  auto n = readln.chomp.to!size_t;
  auto ai = iota(n).map!(_ => readln.chomp.to!size_t).array;

  auto g = new MatGraph(n * 2 + 2);
  foreach (i; 0..n) {
    g[n * 2, i] = 1;
    g[i + n, n * 2 + 1] = 1;
    foreach (j; 0..n)
      if (j != ai[i]) g[i, j + n] = 1;
  }

  auto r = g.fordFulkerson(n * 2, n * 2 + 1);

  auto s = new size_t[](n);
  foreach (i; 0..n) {
    auto t = r.v[i].countUntil!(a => a != 0 && !isInf(a));
    if (t < 0) {
      writeln(-1);
      return;
    }
    s[i] = t - n;
  }

  s.each!(writeln);
}

template Graph(T) {
  T inf = -1;
  auto isInf(T val) { return val == inf; }

  class Mat {
    size_t n; // size
    T[][] v; // values

    this(size_t n) {
      this.n = n;
      v = new T[][](n, n);
    }

    T opIndex(size_t i, size_t j) { return v[i][j]; }
    T opIndexAssign(T w, size_t i, size_t j) { v[i][j] = w; return w; }
  }

  class MatGraph : Mat {
    this(size_t n) {
      super(n);

      foreach (i; 0..n) v[i][] = inf;
      foreach (i; 0..n) v[i][i] = 0;
    }

    auto bfs(size_t s, size_t t) {
      auto visited = new bool[](n);
      visited[s] = true;

      auto si = new DList!(size_t[]);
      si.insertBack([s]);

      while (!si.empty) {
        auto vs = si.front;
        si.removeFront;

        foreach (i; 0..n)
          if (!isInf(this[vs[$-1], i]) && !visited[i]) {
            if (i == t) {
              return vs ~ i;
            } else {
              visited[i] = true;
              si.insertBack(vs ~ i);
            }
          }
      }

      return cast(size_t[])[];
    }

    auto fordFulkerson(size_t s, size_t t) {
      auto c = new MatGraph(n);
      auto r = new MatGraph(n);
      foreach (i; 0..n) r.v[i][] = v[i][];

      auto vs = r.bfs(s, t);
      while (vs.length > 0) {
        auto vn = vs.length - 1;

        auto minW = T.max;
        foreach (i; 0..vn) minW = min(minW, r[vs[i], vs[i+1]]);

        foreach (i; 0..vn) {
          auto u = vs[i], v = vs[i+1];

          if (!isInf(this[u, v])) {
            if (isInf(c[u, v])) c[u, v] = minW;
            else c[u, v] = c[u, v] + minW;
          } else {
            c[v, u] = c[v, u] - minW;
            if (c[v, u] == 0) c[v, u] = inf;
          }

          if (isInf(r[v, u])) r[v, u] = minW;
          else r[v, u] = r[v, u] + minW;

          r[u, v] = r[u, v] - minW;
          if (r[u, v] == 0) r[u, v] = inf;
        }

        vs = r.bfs(s, t);
      }

      return c;
    }
  }
}

mixin Graph!int;
0