結果

問題 No.912 赤黒木
ユーザー たぴちゃんたぴちゃん
提出日時 2019-10-18 14:18:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,204 ms / 3,000 ms
コード長 831 bytes
コンパイル時間 1,063 ms
コンパイル使用メモリ 87,088 KB
実行使用メモリ 434,412 KB
最終ジャッジ日時 2023-09-07 20:37:47
合計ジャッジ時間 29,855 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 139 ms
103,732 KB
testcase_01 AC 138 ms
103,680 KB
testcase_02 AC 174 ms
106,312 KB
testcase_03 AC 191 ms
106,548 KB
testcase_04 AC 147 ms
104,084 KB
testcase_05 AC 175 ms
105,860 KB
testcase_06 AC 175 ms
105,884 KB
testcase_07 AC 188 ms
106,636 KB
testcase_08 AC 135 ms
103,800 KB
testcase_09 AC 176 ms
106,004 KB
testcase_10 AC 149 ms
103,932 KB
testcase_11 AC 170 ms
105,808 KB
testcase_12 AC 974 ms
123,980 KB
testcase_13 AC 1,004 ms
124,844 KB
testcase_14 AC 1,019 ms
124,068 KB
testcase_15 AC 1,171 ms
128,020 KB
testcase_16 AC 1,152 ms
126,220 KB
testcase_17 AC 1,075 ms
124,316 KB
testcase_18 AC 1,140 ms
128,796 KB
testcase_19 AC 1,036 ms
126,900 KB
testcase_20 AC 975 ms
126,708 KB
testcase_21 AC 976 ms
125,228 KB
testcase_22 AC 747 ms
131,340 KB
testcase_23 AC 781 ms
131,544 KB
testcase_24 AC 788 ms
131,700 KB
testcase_25 AC 920 ms
123,708 KB
testcase_26 AC 1,020 ms
125,244 KB
testcase_27 AC 947 ms
122,860 KB
testcase_28 AC 2,118 ms
431,340 KB
testcase_29 AC 2,106 ms
431,084 KB
testcase_30 AC 2,204 ms
434,412 KB
testcase_31 AC 1,935 ms
425,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**7)

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