結果

問題 No.2677 Minmax Independent Set
ユーザー mkawa2mkawa2
提出日時 2024-03-15 23:32:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 742 ms / 2,000 ms
コード長 1,925 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 81,828 KB
実行使用メモリ 165,604 KB
最終ジャッジ日時 2024-03-15 23:32:41
合計ジャッジ時間 23,851 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 39 ms
53,460 KB
testcase_03 AC 39 ms
53,460 KB
testcase_04 AC 39 ms
53,460 KB
testcase_05 AC 631 ms
151,060 KB
testcase_06 AC 576 ms
152,528 KB
testcase_07 AC 615 ms
153,812 KB
testcase_08 AC 603 ms
150,520 KB
testcase_09 AC 602 ms
152,448 KB
testcase_10 AC 574 ms
156,584 KB
testcase_11 AC 593 ms
156,588 KB
testcase_12 AC 466 ms
157,124 KB
testcase_13 AC 646 ms
163,140 KB
testcase_14 AC 605 ms
153,384 KB
testcase_15 AC 684 ms
151,652 KB
testcase_16 AC 727 ms
158,092 KB
testcase_17 AC 655 ms
145,324 KB
testcase_18 AC 476 ms
128,128 KB
testcase_19 AC 517 ms
129,788 KB
testcase_20 AC 210 ms
93,836 KB
testcase_21 AC 586 ms
135,036 KB
testcase_22 AC 124 ms
80,636 KB
testcase_23 AC 50 ms
64,024 KB
testcase_24 AC 39 ms
53,460 KB
testcase_25 AC 40 ms
53,460 KB
testcase_26 AC 40 ms
53,460 KB
testcase_27 AC 47 ms
61,692 KB
testcase_28 AC 546 ms
165,604 KB
testcase_29 AC 604 ms
156,076 KB
testcase_30 AC 38 ms
53,460 KB
testcase_31 AC 37 ms
53,460 KB
testcase_32 AC 38 ms
53,460 KB
testcase_33 AC 38 ms
53,460 KB
testcase_34 AC 38 ms
53,460 KB
testcase_35 AC 39 ms
53,460 KB
testcase_36 AC 40 ms
53,460 KB
testcase_37 AC 42 ms
55,592 KB
testcase_38 AC 57 ms
66,352 KB
testcase_39 AC 82 ms
76,740 KB
testcase_40 AC 99 ms
76,960 KB
testcase_41 AC 100 ms
78,080 KB
testcase_42 AC 117 ms
80,128 KB
testcase_43 AC 130 ms
82,864 KB
testcase_44 AC 195 ms
92,784 KB
testcase_45 AC 359 ms
115,728 KB
testcase_46 AC 675 ms
145,052 KB
testcase_47 AC 727 ms
161,016 KB
testcase_48 AC 701 ms
161,240 KB
testcase_49 AC 742 ms
161,160 KB
testcase_50 AC 242 ms
108,188 KB
testcase_51 AC 88 ms
77,560 KB
testcase_52 AC 427 ms
143,272 KB
testcase_53 AC 471 ms
151,180 KB
testcase_54 AC 80 ms
76,916 KB
testcase_55 AC 543 ms
153,692 KB
testcase_56 AC 571 ms
159,240 KB
testcase_57 AC 531 ms
153,364 KB
testcase_58 AC 557 ms
153,368 KB
testcase_59 AC 520 ms
159,340 KB
testcase_60 AC 337 ms
149,376 KB
testcase_61 AC 334 ms
152,144 KB
testcase_62 AC 316 ms
146,832 KB
testcase_63 AC 323 ms
151,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(1000005)
# 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 << 62)

# md = 10**9+7
md = 998244353

def rerooting(root=0):
    uu = []
    stack = [root]
    while stack:
        u = stack.pop()
        uu.append(u)
        vv = []
        # !!!check the format of the "to"!!!
        for v in to[u]:
            if v == par[u]: continue
            vv.append(v)
            par[v] = u
            stack.append(v)
        to[u] = vv

    # bottom up
    for u in uu[::-1]:
        p = par[u]
        if p == -1: continue
        dp[p] = merge(dp[p], trans_up(dp[u], u))

    # top down
    for u in uu:
        p = par[u]
        if p == -1: continue
        dp[u] = merge(dp[u], trans_down(purge(dp[p], trans_up(dp[u], u)), u))

# v is child
# a is parent's value
# b is child's value
def trans_up(b, v):
    b0,b1=b
    if b0>b1:b1=b0
    return [b0,b1]

def trans_down(a, v):
    b0,b1=a
    if b0>b1:b1=b0
    return [b0,b1]

def merge(a, b):
    aw,ab=a
    bw,bb=b
    return [aw+bb,ab+bw]

def purge(a, b):
    aw,ab=a
    bw,bb=b
    return [aw-bb,ab-bw]

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

par = [-1]*n
dp = [[0,1] for _ in range(n)]
rerooting()
print(min(b for a,b in dp))
0