結果

問題 No.912 赤黒木
ユーザー たぴちゃんたぴちゃん
提出日時 2019-10-18 14:16:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,446 ms / 3,000 ms
コード長 831 bytes
コンパイル時間 2,416 ms
コンパイル使用メモリ 86,940 KB
実行使用メモリ 433,720 KB
最終ジャッジ日時 2023-09-07 20:36:58
合計ジャッジ時間 29,577 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
103,320 KB
testcase_01 AC 149 ms
103,376 KB
testcase_02 AC 195 ms
105,860 KB
testcase_03 AC 208 ms
106,260 KB
testcase_04 AC 158 ms
103,512 KB
testcase_05 AC 191 ms
105,540 KB
testcase_06 AC 188 ms
105,548 KB
testcase_07 AC 205 ms
106,220 KB
testcase_08 AC 153 ms
103,424 KB
testcase_09 AC 188 ms
105,552 KB
testcase_10 AC 179 ms
103,500 KB
testcase_11 AC 182 ms
105,392 KB
testcase_12 AC 986 ms
123,612 KB
testcase_13 AC 1,003 ms
124,892 KB
testcase_14 AC 964 ms
124,188 KB
testcase_15 AC 1,094 ms
127,588 KB
testcase_16 AC 1,068 ms
125,732 KB
testcase_17 AC 1,013 ms
123,864 KB
testcase_18 AC 1,070 ms
128,536 KB
testcase_19 AC 1,037 ms
126,740 KB
testcase_20 AC 1,024 ms
126,080 KB
testcase_21 AC 1,037 ms
124,896 KB
testcase_22 AC 784 ms
131,200 KB
testcase_23 AC 772 ms
131,088 KB
testcase_24 AC 761 ms
131,484 KB
testcase_25 AC 898 ms
123,568 KB
testcase_26 AC 929 ms
124,632 KB
testcase_27 AC 884 ms
122,496 KB
testcase_28 AC 2,218 ms
430,440 KB
testcase_29 AC 2,446 ms
431,836 KB
testcase_30 AC 2,141 ms
433,720 KB
testcase_31 AC 2,018 ms
425,212 KB
権限があれば一括ダウンロードができます

ソースコード

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