結果

問題 No.1424 Ultrapalindrome
ユーザー roarisroaris
提出日時 2021-03-12 21:49:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 191 ms / 2,000 ms
コード長 831 bytes
コンパイル時間 497 ms
コンパイル使用メモリ 86,760 KB
実行使用メモリ 99,920 KB
最終ジャッジ日時 2023-08-04 18:58:53
合計ジャッジ時間 6,236 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,616 KB
testcase_01 AC 92 ms
71,624 KB
testcase_02 AC 91 ms
71,568 KB
testcase_03 AC 92 ms
71,616 KB
testcase_04 AC 91 ms
71,632 KB
testcase_05 AC 91 ms
71,484 KB
testcase_06 AC 90 ms
71,644 KB
testcase_07 AC 93 ms
71,648 KB
testcase_08 AC 93 ms
71,548 KB
testcase_09 AC 191 ms
87,392 KB
testcase_10 AC 172 ms
87,472 KB
testcase_11 AC 145 ms
83,300 KB
testcase_12 AC 163 ms
84,356 KB
testcase_13 AC 121 ms
77,960 KB
testcase_14 AC 157 ms
83,620 KB
testcase_15 AC 95 ms
71,456 KB
testcase_16 AC 139 ms
82,544 KB
testcase_17 AC 144 ms
83,244 KB
testcase_18 AC 151 ms
82,704 KB
testcase_19 AC 169 ms
84,464 KB
testcase_20 AC 127 ms
78,176 KB
testcase_21 AC 168 ms
83,380 KB
testcase_22 AC 144 ms
81,952 KB
testcase_23 AC 110 ms
78,024 KB
testcase_24 AC 119 ms
78,136 KB
testcase_25 AC 121 ms
78,152 KB
testcase_26 AC 182 ms
86,936 KB
testcase_27 AC 176 ms
89,604 KB
testcase_28 AC 140 ms
87,904 KB
testcase_29 AC 152 ms
89,176 KB
testcase_30 AC 166 ms
99,884 KB
testcase_31 AC 176 ms
99,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

def bfs():
    dist = [-1]*N
    dist[l[0]] = 0
    q = deque([l[0]])
    
    while q:
        v = q.popleft()
        
        for nv in G[v]:
            if dist[nv]==-1:
                dist[nv] = dist[v]+1
                q.append(nv)
    
    return dist

N = int(input())
deg = [0]*N
G = [[] for _ in range(N)]

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

if max(deg)<=2:
    print('Yes')
    exit()

l = []

for i in range(N):
    if deg[i]>=3:
        l.append(i)
    
if len(l)>=2:
    print('No')
    exit()

dist = bfs()
dl = []

for i in range(N):
    if deg[i]==1:
        dl.append(dist[i])

if dl==[dl[0]]*len(dl):
    print('Yes')
else:
    print('No')
0