結果

問題 No.1752 Up-Down Tree
ユーザー maguroflymagurofly
提出日時 2021-11-19 22:35:42
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 735 bytes
コンパイル時間 437 ms
コンパイル使用メモリ 11,340 KB
実行使用メモリ 32,712 KB
最終ジャッジ日時 2023-08-30 09:22:07
合計ジャッジ時間 8,181 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 WA -
testcase_03 WA -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 WA -
testcase_09 WA -
testcase_10 RE -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 82 ms
15,052 KB
testcase_21 AC 81 ms
15,112 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
graph = Array.new(N) { [] }
(N - 1).times do
    u, v = gets.split.map { |s| s.to_i - 1}
    graph[u] << v
    graph[v] << u
end

depth = [nil] * N
depth[0] = 0
stack = [0]
while (u = stack.pop)
    graph[u].each do |v|
        next if depth[v]
        depth[v] = depth[u] + 1
        stack << v
    end
end

path = []
visited = [false] * N
dfs = ->u {
    visited[u] = true
    graph[u].each do |v|
        next if visited[v]
        path << v
        dfs[v]
        path << u
    end
}
dfs[0]

mode = -1
visited = [false] * N
ans = 4
u = path[0]
visited[u] = true
path[1..].each do |v|
    next if visited[v] or (depth[u] <=> depth[v]) != mode
    visited[v] = true
    mode = -mode
    ans += 1
    u = v
end

puts ans
0