結果

問題 No.1098 LCAs
ユーザー yuruhiyayuruhiya
提出日時 2020-07-01 16:46:29
言語 Crystal
(1.11.2)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 446 bytes
コンパイル時間 18,689 ms
コンパイル使用メモリ 255,648 KB
実行使用メモリ 43,044 KB
最終ジャッジ日時 2023-09-13 10:56:23
合計ジャッジ時間 23,912 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,496 KB
testcase_01 AC 3 ms
4,544 KB
testcase_02 AC 3 ms
4,576 KB
testcase_03 AC 2 ms
4,516 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,428 KB
testcase_06 AC 3 ms
4,560 KB
testcase_07 AC 2 ms
4,472 KB
testcase_08 AC 2 ms
4,468 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,488 KB
testcase_11 AC 2 ms
4,404 KB
testcase_12 AC 2 ms
4,516 KB
testcase_13 AC 3 ms
4,772 KB
testcase_14 AC 3 ms
4,696 KB
testcase_15 AC 3 ms
4,704 KB
testcase_16 AC 3 ms
4,772 KB
testcase_17 AC 3 ms
4,844 KB
testcase_18 AC 232 ms
32,912 KB
testcase_19 AC 230 ms
32,636 KB
testcase_20 AC 222 ms
32,604 KB
testcase_21 AC 220 ms
32,668 KB
testcase_22 AC 223 ms
32,524 KB
testcase_23 AC 144 ms
38,132 KB
testcase_24 AC 152 ms
36,924 KB
testcase_25 AC 145 ms
37,340 KB
testcase_26 AC 148 ms
39,812 KB
testcase_27 AC 140 ms
40,056 KB
testcase_28 AC 225 ms
42,196 KB
testcase_29 AC 208 ms
43,044 KB
testcase_30 AC 229 ms
41,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = read_line.to_i
g = Array.new(n) { [] of Int32 }
n.pred.times do
  v, w = read_line.split.map &.to_i.pred
  g[v] << w
  g[w] << v
end

def dfs(v : Int32, par : Int32, g : Array(Array(Int32)), ans : Array(Int64))
  child, bottom = 1i64, 0i64
  g[v].select { |i| par != i }.each { |u|
    r = dfs(u, v, g, ans)
    child += r
    bottom += r**2
  }
  ans[v] = child**2 - bottom
  child
end

ans = [0i64]*n
dfs(0, -1, g, ans)
puts ans.join('\n')
0