結果

問題 No.1424 Ultrapalindrome
ユーザー tktk_snsn
提出日時 2021-03-15 14:57:38
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 309 ms / 2,000 ms
コード長 751 bytes
コンパイル時間 393 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 31,872 KB
最終ジャッジ日時 2024-11-07 02:53:17
合計ジャッジ時間 6,190 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

N = int(input())
G = [[] for _ in range(N)]
deg = [0] * N
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)
    deg[a] += 1
    deg[b] += 1

leaf = deg.count(1)
branch = deg.count(2)
if leaf + branch == N:
    print("Yes")
    exit()
if N - leaf - branch > 1:
    print("No")
    exit()

root = deg.index(max(deg))
dep = [-1] * N
q = [root]
dep[root] = 0
L = set()
while q:
    s = q.pop()
    if deg[s] == 1:
        L.add(dep[s])
        continue
    for t in G[s]:
        if dep[t] == -1:
            dep[t] = dep[s] + 1
            q.append(t)

if len(L) == 1:
    print("Yes")
else:
    print("No")
0