結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,148 ms
59,832 KB
testcase_01 AC 1,167 ms
59,444 KB
testcase_02 AC 1,145 ms
59,884 KB
testcase_03 RE -
testcase_04 AC 83 ms
15,036 KB
testcase_05 AC 1,129 ms
60,148 KB
testcase_06 AC 1,136 ms
60,308 KB
testcase_07 AC 1,155 ms
59,868 KB
testcase_08 AC 145 ms
20,020 KB
testcase_09 AC 152 ms
20,100 KB
testcase_10 AC 158 ms
20,008 KB
testcase_11 AC 156 ms
20,076 KB
testcase_12 AC 166 ms
20,192 KB
testcase_13 AC 80 ms
15,048 KB
testcase_14 AC 82 ms
15,288 KB
testcase_15 AC 82 ms
15,164 KB
testcase_16 AC 82 ms
15,192 KB
testcase_17 AC 82 ms
15,052 KB
testcase_18 AC 79 ms
15,108 KB
testcase_19 AC 77 ms
15,268 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

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
0