結果

問題 No.470 Inverse S+T Problem
ユーザー te-shte-sh
提出日時 2017-12-15 11:57:02
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 2,197 bytes
コンパイル時間 622 ms
コンパイル使用メモリ 85,672 KB
実行使用メモリ 6,468 KB
最終ジャッジ日時 2023-09-03 17:37:03
合計ジャッジ時間 2,972 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 21 ms
5,984 KB
testcase_07 AC 20 ms
6,468 KB
testcase_08 AC 21 ms
5,984 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 4 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(16): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`
Main.d(18): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`
Main.d(34): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`
Main.d(36): Deprecation: foreach: loop index implicitly converted from `size_t` to `int`

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto n = readln.chomp.to!int;
  auto u = new string[](n);
  foreach (i; 0..n) u[i] = readln.chomp;

  if (n > 52) {
    writeln("Impossible");
    return;
  }

  auto ts = TwoSAT(n);

  foreach (int i, ui; u) {
    auto sit = ui[0..1], tit = ui[1..3], sif = ui[0..2], tif = ui[2..3];
    foreach (int j, uj; u) {
      if (j <= i) continue;
      auto sjt = uj[0..1], tjt = uj[1..3], sjf = uj[0..2], tjf = uj[2..3];
      if (sit == sjt || tit == tjt) ts.addClause(~i, ~j);
      if (sit == tjf || tit == sjf) ts.addClause(~i, j);
      if (sif == tjt || tif == sjt) ts.addClause(i, ~j);
      if (sif == sjf || tif == tjf) ts.addClause(i, j);
    }
  }

  if (!ts.solve()) {
    writeln("Impossible");
    return;
  }

  auto idx = new int[](n*2);
  foreach (int i, oi; ts.ord) idx[oi+n] = i;

  foreach (int i, ui; u) {
    if (idx[~i+n] > idx[i+n]) writeln(ui[0..2], " ", ui[2..3]);
    else                      writeln(ui[0..1], " ", ui[1..3]);
  }
}

struct TwoSAT
{
  import std.algorithm;
  import std.stdio;

  int n;
  bool[] x;
  int[] num, col, ord;
  int[][] imp, rmp;

  this(int n)
  {
    this.n = n;
    auto n2 = n * 2;
    x = new bool[](n2);
    num = new int[](n2);
    col = new int[](n2);
    imp = new int[][](n2);
    rmp = new int[][](n2);
  }

  auto addClause(int u, int v)
  {
    if (u == ~v) {
      return;
    } else if (u == v) {
      x[u+n] = true;
    } else {
      imp[~u+n] ~= v;
      imp[~v+n] ~= u;
      rmp[u+n] ~= ~v;
      rmp[v+n] ~= ~u;
    }
  }

  auto solve() {
    foreach (u; -n..n)
      if (x[u+n]) visit(u, false);
    foreach (u; -n..n)
      if (x[u+n] && x[~u+n]) return false;
      else visit(u, 0);
    num = col.dup;
    ord.reverse();
    foreach (v; ord) rvisit(v, v);
    foreach (u; 0..n)
      if (col[u+n] == col[~u+n]) return false;
    return true;
  }

  private auto visit(int u, bool b)
  {
    if (num[u+n]++ > 0) return;
    x[u+n] |= b;
    foreach (v; imp[u+n]) visit(v, x[u+n]);
    ord ~= u;
  }

  private auto rvisit(int u, int k)
  {
    if (num[u+n]++ > 0) return;
    col[u+n] = k;
    foreach (v; rmp[u+n]) rvisit(v, k);
  }
}
0