結果

問題 No.912 赤黒木
ユーザー mkawa2mkawa2
提出日時 2023-09-21 23:48:01
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,817 bytes
コンパイル時間 310 ms
コンパイル使用メモリ 87,108 KB
実行使用メモリ 189,448 KB
最終ジャッジ日時 2023-09-21 23:48:22
合計ジャッジ時間 20,033 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,108 KB
testcase_01 AC 78 ms
71,304 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 848 ms
189,296 KB
testcase_23 AC 816 ms
189,448 KB
testcase_24 AC 806 ms
189,152 KB
testcase_25 AC 863 ms
156,364 KB
testcase_26 AC 896 ms
150,228 KB
testcase_27 AC 811 ms
147,908 KB
testcase_28 AC 724 ms
133,772 KB
testcase_29 AC 691 ms
129,404 KB
testcase_30 AC 795 ms
132,004 KB
testcase_31 AC 750 ms
129,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 63)
# md = 10**9+7
md = 998244353

n = II()
deg = [0]*n
to = [[] for _ in range(n)]
for _ in range(n-1):
    u, v = LI1()
    to[u].append(v)
    to[v].append(u)
for _ in range(n-1):
    u, v = LI1()
    deg[u] ^= 1
    deg[v] ^= 1

def dfs(root=0):
    uu, stack = [], [root]
    while stack:
        u = stack.pop()
        uu.append(u)
        for v in to[u]:
            if v == parent[u]: continue
            parent[v] = u
            depth[v] = depth[u]+1
            stack.append(v)
    return uu

parent, depth = [-1]*n, [0]*n
uu = dfs()
to2 = [[] for _ in range(n)]

ans = n-1
for u in uu[:0:-1]:
    if deg[u]:
        p = parent[u]
        to2[u].append(p)
        to2[p].append(u)
        ans += 1
        deg[p] ^= 1

to = to2
fin = [0]*n
parent, depth = [-1]*n, [0]*n
mx = 0
for u in range(n):
    if fin[u]: continue
    uu = dfs(u)
    mxd = mxu = -1
    for u in uu:
        fin[u] = 1
        if depth[u] > mxd:
            mxd = depth[u]
            mxu = u
    depth[mxu] = 0
    parent[mxu] = -1
    dfs(mxu)
    for u in uu: mx = max(mx, depth[u])

print(ans-mx)
0