結果

問題 No.1424 Ultrapalindrome
ユーザー wolgnik
提出日時 2021-03-12 22:17:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 725 bytes
コンパイル時間 405 ms
コンパイル使用メモリ 82,440 KB
実行使用メモリ 99,388 KB
最終ジャッジ日時 2024-10-14 16:19:13
合計ジャッジ時間 4,185 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
N = int(input())
e = [[] for _ in range(N + 1)]
inc = [0] * (N + 1)
for _ in range(N - 1):
  u, v = map(int, input().split())
  e[u].append(v)
  e[v].append(u)
  inc[u] += 1
  inc[v] += 1

if max(inc) <= 2:
  print("Yes")
  exit(0)

root = 1
for x in range(1, N + 1):
  if inc[root] < inc[x]: root = x

s = [root]
vis = [0] * (N + 1)
vis[root] = 1
depth = [0] * (N + 1)
while len(s):
  x = s.pop()
  for y in e[x]:
    if vis[y]: continue
    vis[y] = 1
    s.append(y)
    if inc[y] > 2:
      print("No")
      exit(0)
    depth[y] = depth[x] + 1

leaves = []
for x in range(1, N + 1):
  if inc[x] == 1: leaves.append(depth[x])

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