結果

問題 No.277 根掘り葉掘り
ユーザー te-shte-sh
提出日時 2017-06-07 18:13:08
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 404 ms / 3,000 ms
コード長 1,082 bytes
コンパイル時間 911 ms
コンパイル使用メモリ 94,092 KB
実行使用メモリ 25,536 KB
最終ジャッジ日時 2023-09-03 13:58:47
合計ジャッジ時間 6,522 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 206 ms
13,684 KB
testcase_10 AC 203 ms
19,324 KB
testcase_11 AC 284 ms
13,072 KB
testcase_12 AC 404 ms
13,920 KB
testcase_13 AC 395 ms
24,276 KB
testcase_14 AC 343 ms
24,980 KB
testcase_15 AC 376 ms
24,748 KB
testcase_16 AC 380 ms
23,744 KB
testcase_17 AC 379 ms
24,956 KB
testcase_18 AC 373 ms
24,476 KB
testcase_19 AC 391 ms
25,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap

void main()
{
  auto n = readln.chomp.to!size_t;
  auto ei = new size_t[][](n);
  foreach (_; 0..n-1) {
    auto rd = readln.split.to!(size_t[]).map!"a - 1", i = rd[0], j = rd[1];
    ei[i] ~= j;
    ei[j] ~= i;
  }

  auto fi = new int[](n);
  auto gi = new bool[](n);

  struct E { size_t u, p; int l; }

  auto si = new SList!E(E(0, n, 0));
  while (!si.empty) {
    auto s = si.front; si.removeFront();

    auto leaf = true;
    foreach (e; ei[s.u]) {
      if (e == s.p) continue;
      leaf = false;
      fi[e] = s.l + 1;
      si.insertFront(E(e, s.u, s.l + 1));
    }

    gi[s.u] = leaf;
  }

  foreach (i; 0..n) {
    if (!gi[i]) continue;

    fi[i] = 0;
    auto ti = new SList!E(E(i, n, 0));
    while (!ti.empty) {
      auto t = ti.front; ti.removeFront();

      foreach (e; ei[t.u]) {
        if (e == t.p || fi[e] <= t.l + 1) continue;
        fi[e] = t.l + 1;
        ti.insertFront(E(e, t.u, t.l + 1));
      }
    }
  }

  fi.each!writeln;
}
0