結果

問題 No.912 赤黒木
ユーザー たぴちゃん
提出日時 2019-10-18 14:16:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,267 ms / 3,000 ms
コード長 831 bytes
コンパイル時間 297 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 426,880 KB
最終ジャッジ日時 2024-06-25 14:20:19
合計ジャッジ時間 29,940 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**6)

INF = 1000000000
G = [[] for i in range(200000)]
deg = [0] * 200000
dp = [[0 for j in range(3)] for i in range(200000)]

def calc(v, p):
    for to in G[v]:
        if to == p:
            continue
        calc(to, v)
        deg[v] += deg[to]
        nxt = [INF] * 3
        for i in range(3):
            for j in range(3 - i):
                nxt[i + j] = min(nxt[i + j], dp[v][i] + dp[to][j] + (deg[to] % 2 != j % 2))
        for i in range(3):
            dp[v][i] = nxt[i]
    return

N = int(input())
for i in range(N - 1):
    A, B = [int(i) for i in input().split()]
    G[A - 1].append(B - 1)
    G[B - 1].append(A - 1)

for i in range(N - 1):
    C, D = [int(i) for i in input().split()]
    deg[C - 1] += 1
    deg[D - 1] += 1

calc(0, -1)
print(min(dp[0][0], dp[0][2]) + N - 1)
0