結果
| 問題 |
No.1484 木に数を書き込む問題 / Just Write Numbers! 2
|
| ユーザー |
|
| 提出日時 | 2021-05-12 02:40:10 |
| 言語 | Crystal (1.14.0) |
| 結果 |
AC
|
| 実行時間 | 442 ms / 2,000 ms |
| コード長 | 298 bytes |
| コンパイル時間 | 13,462 ms |
| コンパイル使用メモリ | 295,924 KB |
| 実行使用メモリ | 63,032 KB |
| 最終ジャッジ日時 | 2024-09-22 08:43:35 |
| 合計ジャッジ時間 | 21,047 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
def dfs(g,z,c)
f=1
r=[0,c]
g[c].each{|e|
next if e==z
t=dfs(g,c,e)
r=[r,[t[0]+f,t[1]]].max
}
r
end
def diameter(g)
dfs(g,-1,dfs(g,-1,0)[1])[0]
end
n=gets.to_s.to_i
g=n.times.map{[0]*0}.to_a
(n-1).times{
a,b=gets.to_s.split.map{|e|e.to_i}
g[a-1]<<b-1
g[b-1]<<a-1
}
p 2*~-n-diameter(g)