結果

問題 No.912 赤黒木
ユーザー maspymaspy
提出日時 2020-04-22 18:21:40
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,414 bytes
コンパイル時間 315 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 81,948 KB
最終ジャッジ日時 2024-04-20 03:30:29
合計ジャッジ時間 8,293 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
16,640 KB
testcase_01 AC 27 ms
10,880 KB
testcase_02 AC 62 ms
11,136 KB
testcase_03 AC 81 ms
11,264 KB
testcase_04 AC 48 ms
10,880 KB
testcase_05 AC 69 ms
11,008 KB
testcase_06 AC 57 ms
11,264 KB
testcase_07 AC 85 ms
11,136 KB
testcase_08 AC 31 ms
11,008 KB
testcase_09 AC 61 ms
11,008 KB
testcase_10 AC 41 ms
10,880 KB
testcase_11 AC 57 ms
11,136 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import itertools

N = int(readline())
graph = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    a, b = map(int, readline().split())
    graph[a].append(b)
    graph[b].append(a)

deg = [0] * (N + 1)
for _ in range(N - 1):
    a, b = map(int, readline().split())
    deg[a] += 1
    deg[b] += 1

root = 1
parent = [0] * (N + 1)
order = []
stack = [root]
while stack:
    x = stack.pop()
    order.append(x)
    for y in graph[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        stack.append(y)

INF = 10 ** 9
dp = [[0, INF, INF, INF, INF, INF] for _ in range(N + 1)]

for v in order[::-1]:
    a, b, c, d, e, f = dp[v]
    if deg[v] & 1:
        dp[v] = [
            d, min(a, e), min(b, f),
            a + 1, min(b, d) + 1, min(c, e) + 1
        ]
    else:
        dp[v] = [
            a, min(b, d), min(c, e),
            d + 1, min(a, e) + 1, min(b, f) + 1
        ]
    if v == root:
        break
    p = parent[v]
    A = [INF, INF, INF, INF, INF, INF]
    for a, b in itertools.product(range(6), repeat=2):
        i, k = divmod(a, 3)
        j, l = divmod(b, 3)
        i ^= j
        k += l
        if k > 2:
            continue
        c = 3 * i + k
        A[c] = min(A[c], dp[v][a] + dp[p][b])
    dp[p] = A

x = min(dp[root][:3])
print(N - 1 + x)
0