結果

問題 No.1424 Ultrapalindrome
コンテスト
ユーザー GER_chen
提出日時 2021-03-12 21:56:37
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 202 ms / 2,000 ms
コード長 1,134 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 198 ms
コンパイル使用メモリ 85,632 KB
実行使用メモリ 130,220 KB
最終ジャッジ日時 2026-05-02 02:40:57
合計ジャッジ時間 4,535 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys;input = lambda: sys.stdin.readline().rstrip()
n = int(input())
Adj = [set() for _ in range(n)]
Leaves = set()
for i in range(n-1):
    a, b = map(int,input().split())
    a -= 1
    b -= 1
    Adj[a].add(b)
    Adj[b].add(a)
    if a not in Leaves:
        if len(Adj[a]) == 1:
            Leaves.add(a)
    else:
        Leaves.remove(a)
    if b not in Leaves:
        if len(Adj[b]) == 1:
            Leaves.add(b)
    else:
        Leaves.remove(b)

n_l = len(Leaves)
if n_l == 2:
    print('Yes')
    exit()
if n%n_l != 1:
    print('No')
    exit()

ans = 'Yes'
Vertices = set(range(n))
while Vertices and n > 2:
    nxtLeaves = set()
    Candidates = set()
    for l in Leaves:
        Vertices.remove(l)
        for x in Adj[l]:
            Candidates.add(x)
            if len(Adj[x]) == 2:
                nxtLeaves.add(x)
    if nxtLeaves == set() and len(Candidates) == 1:
        break
    elif len(nxtLeaves) == n_l:
        for nl in nxtLeaves:
            Adj[nl] = [a for a in Adj[nl] if a not in Leaves]
        Leaves = nxtLeaves
    else:
        ans = 'No'
        break
    n -= len(Leaves)
print(ans)
0