結果

問題 No.1424 Ultrapalindrome
ユーザー marroncastle
提出日時 2021-03-12 22:02:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 1,162 bytes
コンパイル時間 209 ms
コンパイル使用メモリ 82,172 KB
実行使用メモリ 95,544 KB
最終ジャッジ日時 2024-10-14 16:16:25
合計ジャッジ時間 4,231 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import deque
class Tree:
  def __init__(self, N):
    self.V = N
    self.edge = [[] for _ in range(N)]
    self.order = []
    self.num = [0]*N
  
  def add_edges(self, ind=1, bi=True):
    for i in range(self.V-1):
      a,b = map(int, input().split())
      a -= ind; b -= ind
      self.edge[a].append(b)
      self.num[a] += 1
      if bi:
        self.edge[b].append(a)
        self.num[b] += 1

  def add_edge(self, a, b, bi=True):
    self.edge[a].append(b)
    if bi: self.edge[b].append(a)

  def bfs(self, start):
    d = deque([start])
    self.min_cost = [-1]*self.V; self.min_cost[start]=0
    s = set()
    while len(d)>0:
      v = d.popleft()
      if self.num[v]==1:
        s.add(self.min_cost[v])
      for w in self.edge[v]:
        if self.min_cost[w]==-1:
          self.min_cost[w]=self.min_cost[v]+1
          d.append(w)
    return len(s)

N = int(input())
G = Tree(N)
G.add_edges(ind=1, bi=True)
lis = [int(l>=3) for l in G.num]
cnt = sum(lis)
if cnt>1: print('No')
elif cnt==0: print('Yes')
else:
  ind = lis.index(1)
  if G.bfs(ind)==1:
    print('Yes')
  else:
    print('No')
0