結果

問題 No.912 赤黒木
ユーザー たぴちゃんたぴちゃん
提出日時 2019-10-18 16:07:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,033 ms / 3,000 ms
コード長 979 bytes
コンパイル時間 263 ms
コンパイル使用メモリ 87,136 KB
実行使用メモリ 162,312 KB
最終ジャッジ日時 2023-09-07 20:44:56
合計ジャッジ時間 21,909 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,256 KB
testcase_01 AC 76 ms
71,516 KB
testcase_02 AC 117 ms
77,548 KB
testcase_03 AC 143 ms
77,880 KB
testcase_04 AC 86 ms
76,076 KB
testcase_05 AC 123 ms
77,912 KB
testcase_06 AC 119 ms
77,612 KB
testcase_07 AC 138 ms
77,892 KB
testcase_08 AC 75 ms
71,188 KB
testcase_09 AC 114 ms
77,268 KB
testcase_10 AC 84 ms
75,912 KB
testcase_11 AC 106 ms
77,572 KB
testcase_12 AC 905 ms
131,352 KB
testcase_13 AC 1,033 ms
135,716 KB
testcase_14 AC 884 ms
128,436 KB
testcase_15 AC 974 ms
132,432 KB
testcase_16 AC 906 ms
134,336 KB
testcase_17 AC 938 ms
135,584 KB
testcase_18 AC 926 ms
134,048 KB
testcase_19 AC 890 ms
130,712 KB
testcase_20 AC 890 ms
131,132 KB
testcase_21 AC 882 ms
131,012 KB
testcase_22 AC 842 ms
162,312 KB
testcase_23 AC 926 ms
160,608 KB
testcase_24 AC 790 ms
160,464 KB
testcase_25 AC 931 ms
131,356 KB
testcase_26 AC 955 ms
132,444 KB
testcase_27 AC 913 ms
134,844 KB
testcase_28 AC 935 ms
134,356 KB
testcase_29 AC 931 ms
133,612 KB
testcase_30 AC 956 ms
134,228 KB
testcase_31 AC 880 ms
130,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
G = [[] for i in range(N)]
deg = [0] * N
parent = [-1] * N
dp = [[0 for j in range(3)] for i in range(N)]

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

stack = [0]
stack2 = []
parent[0] = 0

while stack:
    v = stack.pop()
    stack2.append(v)
    for to in G[v]:
        if parent[to] != -1:
            continue
        parent[to] = v
        stack.append(to)

while stack2:
    v = stack2.pop()
    for to in G[v]:
        if to == parent[v]:
            continue
        deg[v] += deg[to]
        nxt = [1000000000] * 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]

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