結果

問題 No.872 All Tree Path
ユーザー simansiman
提出日時 2021-08-02 18:20:00
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 787 bytes
コンパイル時間 131 ms
コンパイル使用メモリ 11,244 KB
実行使用メモリ 109,432 KB
最終ジャッジ日時 2023-10-14 20:50:49
合計ジャッジ時間 13,665 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,069 ms
59,596 KB
testcase_01 AC 1,099 ms
59,568 KB
testcase_02 AC 1,126 ms
59,980 KB
testcase_03 RE -
testcase_04 AC 152 ms
15,224 KB
testcase_05 AC 1,184 ms
60,380 KB
testcase_06 AC 1,181 ms
60,332 KB
testcase_07 AC 1,188 ms
60,068 KB
testcase_08 AC 235 ms
20,280 KB
testcase_09 AC 228 ms
20,196 KB
testcase_10 AC 229 ms
20,100 KB
testcase_11 AC 230 ms
20,032 KB
testcase_12 AC 229 ms
20,252 KB
testcase_13 AC 153 ms
15,292 KB
testcase_14 AC 153 ms
15,304 KB
testcase_15 AC 153 ms
15,340 KB
testcase_16 AC 154 ms
15,268 KB
testcase_17 AC 151 ms
15,216 KB
testcase_18 AC 154 ms
15,332 KB
testcase_19 AC 152 ms
15,140 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

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

  (N - 1).times do
    u, v, w = gets.split.map(&:to_i)

    E[u] << [v, w]
    E[v] << [u, w]
  end

  def dfs1(u, cnt = 0, parent = -1)
    cnt = 0

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

      v_cnt = dfs1(v, cnt, u)
      cnt += v_cnt
      V[u] << v_cnt
    end

    cnt + 1
  end

  $ans = 0

  def dfs2(u, v_cnt = 0, parent = -1)
    t_cnt = V[u].sum + v_cnt
    i = 0

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

      cnt = V[u][i]
      $ans += 2 * w * (N - cnt) * cnt
      dfs2(v, t_cnt - cnt + 1, u)
      i += 1
    end
  end

  dfs1(1)
  dfs2(1)

  puts $ans
end
0