結果

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

ソースコード

diff #
raw source code

n = int(input())
node = [[] for _ in range(n)]
A = [-1] * n
B = [0] * n
C = [0] * n
for _ in range(n-1):
    u, v = list(map(lambda x: int(x)-1, input().split()))
    node[u].append(v)
    node[v].append(u)
    B[u] += 1
    B[v] += 1
if n == 2: print(1); exit()

S = [i for i in range(n) if B[i] == 1]
for s in S: A[s] = 0
ans = 0
while S:
    now = S.pop()
    #print(now, A[now], C[now], ans)
    if A[now] == -1:
        if not len(node[now]) % 2:
            if C[now] * 2 > len(node[now]):
                A[now] = 1
            elif C[now] * 2 == len(node[now]):
                A[now] = 0
            else:
                A[now] = 1
                ans += 1
        else:
            if C[now] * 2 > len(node[now]):
                A[now] = 1
            elif C[now] * 2 + 1 == len(node[now]):
                A[now] = 0
            else:
                A[now] = 1
                ans += 1
    for nxt in node[now]:
        if B[nxt]:
            B[now] -= 1
            B[nxt] -= 1
            if A[now] == 1:
                C[nxt] += 1
            if not B[nxt] - 1:
                S.append(nxt)
print(ans)
0