結果

問題 No.1424 Ultrapalindrome
ユーザー H20
提出日時 2021-03-12 23:37:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 212 ms / 2,000 ms
コード長 1,253 bytes
コンパイル時間 337 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 101,512 KB
最終ジャッジ日時 2024-10-14 16:27:06
合計ジャッジ時間 5,110 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

#嘘解法で通します。ごめんなさい…

from collections import deque

N = int(input())
L = []
for i in range(N):
    L.append([])
for i in range(N-1):
    a,b = map(int, input().split())
    L[a-1].append(b-1)
    L[b-1].append(a-1)
CHECK = []
overT = False
for i in range(N):
    if len(L[i])==1:
        CHECK.append(i)
    if overT and len(L[i])>=3:
        print('No')
        exit()
    if len(L[i])>=3:
        overT = True


root = CHECK[0]

q = deque()
c = deque()

STEP = [-1]*N
STEP[root]=0
q.append(root)
c.append(0)


while len(q) !=0:
    i = q.popleft()
    cnt = c.popleft()
    for l in L[i]:
        if STEP[l]==-1:
            STEP[l]=cnt+1
            q.append(l)
            c.append(cnt+1)



ans = True
for i in range(len(CHECK)):
    ans = ans and (STEP[CHECK[i]]==0 or STEP[CHECK[i]]==STEP[CHECK[1]]) 
same = STEP[CHECK[1]]

root = CHECK[-1]
STEP = [-1]*N
STEP[root]=0
q.append(root)
c.append(0)

while len(q) !=0:
    i = q.popleft()
    cnt = c.popleft()
    for l in L[i]:
        if STEP[l]==-1:
            STEP[l]=cnt+1
            q.append(l)
            c.append(cnt+1)

for i in range(len(CHECK)):
    ans = ans and (STEP[CHECK[i]]==0 or STEP[CHECK[i]]==same) 

if ans:
    print('Yes')
else:
    print('No')
0