結果

問題 No.1817 Reversed Edges
ユーザー simansiman
提出日時 2023-02-14 10:36:17
言語 Ruby
(3.3.0)
結果
AC  
実行時間 926 ms / 2,000 ms
コード長 1,015 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 11,540 KB
実行使用メモリ 284,824 KB
最終ジャッジ日時 2023-09-23 19:48:02
合計ジャッジ時間 13,925 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
外部呼び出し有り
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 158 ms
15,420 KB
testcase_01 AC 157 ms
15,276 KB
testcase_02 AC 157 ms
15,240 KB
testcase_03 AC 156 ms
15,388 KB
testcase_04 AC 157 ms
15,132 KB
testcase_05 AC 158 ms
15,248 KB
testcase_06 AC 157 ms
15,376 KB
testcase_07 AC 561 ms
30,824 KB
testcase_08 AC 342 ms
22,912 KB
testcase_09 AC 530 ms
30,072 KB
testcase_10 AC 365 ms
23,732 KB
testcase_11 AC 427 ms
27,296 KB
testcase_12 AC 610 ms
32,156 KB
testcase_13 AC 615 ms
32,140 KB
testcase_14 AC 614 ms
31,992 KB
testcase_15 AC 613 ms
32,196 KB
testcase_16 AC 615 ms
32,036 KB
testcase_17 AC 618 ms
32,016 KB
testcase_18 AC 622 ms
32,212 KB
testcase_19 AC 621 ms
32,096 KB
testcase_20 AC 625 ms
32,280 KB
testcase_21 AC 629 ms
31,952 KB
testcase_22 AC 498 ms
30,464 KB
testcase_23 AC 497 ms
30,420 KB
testcase_24 AC 926 ms
284,824 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

if !ENV['RUBY_THREAD_VM_STACK_SIZE']
  exec({'RUBY_THREAD_VM_STACK_SIZE'=>'1000000000'}, '/usr/bin/ruby', $0)
else
  N = gets.to_i
  E = Array.new(N + 1) { [] }

  (N - 1).times do
    a, b = gets.split.map(&:to_i)

    E[a] << b
    E[b] << a
  end

  DP1 = Array.new(N + 1) { [] }

  def dfs(v, parent, ans)
    r_cnt = 0

    E[v].each do |u|
      next if u == parent

      r = dfs(u, v, ans)
      DP1[v] << r
      r_cnt += r
    end

    ans[v] += r_cnt

    if v < parent
      r_cnt += 1
    end

    r_cnt
  end

  def r_dfs(v, cnt, parent, ans)
    if parent != -1
      ans[v] += cnt
    end

    idx = 0
    E[v].each_with_index do |u|
      next if u == parent

      if v < u
        cnt += 1
      end

      nv = ans[v] - DP1[v][idx]
      nv += 1 if v < u
      r_dfs(u, nv, v, ans)
      idx += 1

      if v < u
        cnt -= 1
      end
    end
  end

  ans = Array.new(N + 1, 0)
  root = 1
  dfs(root, -1, ans)
  r_dfs(root, ans[root], -1, ans)

  1.upto(N) do |v|
    puts ans[v]
  end
end
0