結果

問題 No.1424 Ultrapalindrome
ユーザー marroncastlemarroncastle
提出日時 2021-03-12 22:02:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 1,162 bytes
コンパイル時間 504 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 95,920 KB
最終ジャッジ日時 2024-04-22 17:07:58
合計ジャッジ時間 4,200 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
53,760 KB
testcase_01 AC 42 ms
53,888 KB
testcase_02 AC 41 ms
53,888 KB
testcase_03 AC 41 ms
53,760 KB
testcase_04 AC 42 ms
53,760 KB
testcase_05 AC 41 ms
53,760 KB
testcase_06 AC 42 ms
54,016 KB
testcase_07 AC 41 ms
53,632 KB
testcase_08 AC 42 ms
53,888 KB
testcase_09 AC 121 ms
86,016 KB
testcase_10 AC 124 ms
85,376 KB
testcase_11 AC 102 ms
81,152 KB
testcase_12 AC 125 ms
84,992 KB
testcase_13 AC 92 ms
79,232 KB
testcase_14 AC 125 ms
83,968 KB
testcase_15 AC 44 ms
54,272 KB
testcase_16 AC 95 ms
80,384 KB
testcase_17 AC 100 ms
81,664 KB
testcase_18 AC 114 ms
80,640 KB
testcase_19 AC 132 ms
84,608 KB
testcase_20 AC 87 ms
78,464 KB
testcase_21 AC 117 ms
81,280 KB
testcase_22 AC 104 ms
80,256 KB
testcase_23 AC 68 ms
70,400 KB
testcase_24 AC 85 ms
78,208 KB
testcase_25 AC 84 ms
78,464 KB
testcase_26 AC 151 ms
85,888 KB
testcase_27 AC 149 ms
91,264 KB
testcase_28 AC 102 ms
86,272 KB
testcase_29 AC 118 ms
90,112 KB
testcase_30 AC 129 ms
95,196 KB
testcase_31 AC 129 ms
95,920 KB
権限があれば一括ダウンロードができます

ソースコード

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