結果

問題 No.1098 LCAs
ユーザー yuruhiyayuruhiya
提出日時 2020-07-01 16:55:04
言語 Crystal
(1.11.2)
結果
AC  
実行時間 273 ms / 2,000 ms
コード長 571 bytes
コンパイル時間 18,584 ms
コンパイル使用メモリ 255,548 KB
実行使用メモリ 44,412 KB
最終ジャッジ日時 2023-09-13 10:56:47
合計ジャッジ時間 23,553 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,504 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 3 ms
4,488 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 2 ms
4,404 KB
testcase_06 AC 3 ms
4,412 KB
testcase_07 AC 3 ms
4,420 KB
testcase_08 AC 3 ms
4,516 KB
testcase_09 AC 2 ms
4,432 KB
testcase_10 AC 2 ms
4,572 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 4 ms
4,680 KB
testcase_14 AC 3 ms
4,784 KB
testcase_15 AC 3 ms
4,752 KB
testcase_16 AC 3 ms
4,752 KB
testcase_17 AC 3 ms
4,792 KB
testcase_18 AC 249 ms
32,500 KB
testcase_19 AC 246 ms
32,348 KB
testcase_20 AC 253 ms
32,500 KB
testcase_21 AC 248 ms
32,564 KB
testcase_22 AC 244 ms
32,524 KB
testcase_23 AC 164 ms
37,044 KB
testcase_24 AC 167 ms
37,308 KB
testcase_25 AC 167 ms
37,300 KB
testcase_26 AC 165 ms
39,252 KB
testcase_27 AC 156 ms
39,376 KB
testcase_28 AC 272 ms
42,092 KB
testcase_29 AC 273 ms
44,412 KB
testcase_30 AC 271 ms
41,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Solve
  def initialize
    @n = Int32.new read_line.to_i
    @g = Array(Array(Int32)).new(@n) { [] of Int32 }
    @n.pred.times do
      v, w = read_line.split.map &.to_i.pred
      @g[v] << w
      @g[w] << v
    end
    @ans = Array(Int64).new(@n, 0i64)
  end

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

  def solve
    dfs(0, -1)
    puts @ans.join('\n')
  end
end

Solve.new.solve
0