結果

問題 No.1424 Ultrapalindrome
ユーザー tobusakana
提出日時 2022-10-14 21:31:17
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 844 bytes
コンパイル時間 180 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 98,456 KB
最終ジャッジ日時 2024-06-26 13:13:55
合計ジャッジ時間 6,080 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
G = [[] for i in range(N)]
indegree = [0] * N
for _ in range(N - 1):
    a,b = map(int,input().split())
    G[a - 1].append(b - 1)
    G[b - 1].append(a - 1)
    indegree[a - 1] += 1
    indegree[b - 1] += 1
    
# 次数1のある点から、他の全ての点への距離が同じであればOK
start = -1
for v in range(N):
    if indegree[v] == 1:
        start = v
        break

stack = []
dist = [-1] * N
stack.append([start, 0])
while stack:
    v, d = stack.pop()
    if dist[v] != -1:
        continue
    dist[v] = d
    for child in G[v]:
        if dist[child] != -1:
            continue
        stack.append([child, d + 1])
    
ds = set()
for v in range(N):
    if v == start:
        continue
    if indegree[v] == 1:
        ds.add(dist[v])
        
if len(ds) > 1:
    print("No")
else:
    print("Yes")
    
0