結果

問題 No.497 入れ子の箱
ユーザー te-sh
提出日時 2017-12-25 11:28:53
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 60 ms / 5,000 ms
コード長 1,091 bytes
コンパイル時間 754 ms
コンパイル使用メモリ 112,848 KB
実行使用メモリ 11,852 KB
最終ジャッジ日時 2024-06-12 23:15:31
合計ジャッジ時間 3,044 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

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

alias graph = Graph!int;

void main()
{
  auto n = readln.chomp.to!int;
  auto s = new int[][](n, 3);
  foreach (i; 0..n) {
    s[i] = readln.split.to!(int[]);
    s[i].sort();
  }

  auto g = new int[][](n);
  foreach (i; 0..n)
    foreach (j; 0..n)
      if (iota(3).all!(k => s[i][k] > s[j][k])) g[i] ~= j;

  auto ts = graph.topologicalSort(g);

  auto dp = new int[](n);
  dp[] = 1;

  foreach_reverse (i; ts)
    foreach (j; g[i])
      dp[i] = max(dp[i], dp[j]+1);

  writeln(dp.maxElement);
}

template Graph(Node)
{
  import std.container;

  Node[] topologicalSort(Node[][] g)
  {
    auto n = cast(Node)(g.length), h = new size_t[](n);

    foreach (u; 0..n)
      foreach (v; g[u])
        ++h[v];

    auto st = SList!Node();
    foreach (i; 0..n)
      if (h[i] == 0) st.insertFront(i);

    Node[] ans;
    while (!st.empty()) {
      auto u = st.front; st.removeFront();
      ans ~= u;
      foreach (v; g[u]) {
        --h[v];
        if (h[v] == 0) st.insertFront(v);
      }
    }

    return ans;
  }
}
0