結果

問題 No.3514 Majority Driven Tree
コンテスト
ユーザー detteiuu
提出日時 2026-04-24 23:02:53
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 1,555 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 243 ms
コンパイル使用メモリ 85,272 KB
実行使用メモリ 85,316 KB
最終ジャッジ日時 2026-04-24 23:03:02
合計ジャッジ時間 9,111 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 2 WA * 35
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from sys import stdin
input = stdin.readline
from types import GeneratorType

def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        to = f(*args, **kwargs)
        while True:
            if type(to) is GeneratorType:
                stack.append(to)
                to = next(to)
            else:
                stack.pop()
                if not stack:
                    break
                to = stack[-1].send(to)
        return to
    return wrappedfunc

INF = 1<<60

N = int(input())
G = [[] for _ in range(N)]
for _ in range(N-1):
    u, v = map(int, input().split())
    u, v = u-1, v-1
    G[u].append(v)
    G[v].append(u)

start = -1
for i in range(N):
    if 2 <= len(G[i]):
        start = i
        break

@bootstrap
def dfs(n, p):
    A = []
    for v in G[n]:
        if v == p: continue
        yield dfs(v, n)
        A.append(tuple(dp[v]))
    if not A:
        dp[n][2] = 0
        yield
    # 塗る場合
    SUM = sum(min(a) for a in A)
    dp[n][0] = SUM+1
    B = []
    for a in A:
        B.append(a[0]-min(a))
    B.sort()
    # 塗らない場合(親から黒をもらわなくていい場合)
    cnt = len(G[n])//2+1
    if cnt <= len(B):
        dp[n][1] = SUM+sum(B[:cnt])
    # 塗らない場合(親から黒をもらう必要がある場合)
    if p == -1: yield
    cnt = len(G[n])//2
    if cnt <= len(B):
        dp[n][2] = SUM+sum(B[:cnt])
    yield

dp = [[INF]*3 for _ in range(N)]
dfs(start, -1)

print(min(dp[start][:2]))
0