結果

問題 No.1424 Ultrapalindrome
ユーザー AEnAEn
提出日時 2022-11-30 23:12:16
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,147 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 82,144 KB
実行使用メモリ 67,292 KB
最終ジャッジ日時 2024-04-16 18:45:30
合計ジャッジ時間 4,778 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
61,048 KB
testcase_01 AC 42 ms
55,132 KB
testcase_02 AC 42 ms
54,448 KB
testcase_03 AC 41 ms
54,656 KB
testcase_04 AC 42 ms
54,864 KB
testcase_05 AC 41 ms
53,984 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque

N = int(input())
G = [list() for _ in range(N)]

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

s = []
for i in range(N):
    if len(G[i])==1:
        s.append(i)
if len(s)==2:
    print('Yes')
else:
    dis = [-1]*N
    pre = [-1]*N
    d = deque()
    d.append(s[0])
    dis[s[0]] = 0
    while d:
        v = d.popleft()
        for nex in G[v]:
            if dis[nex]==-1:
                dis[nex]=dis[v]+1
                pre[nex] = v
                d.append(nex)
    if dis[s[1]]%2==1:
        exit(print('No'))
    else:
        dd = dis[s[1]]//2
        nn = dis[s[1]]
        v = s[1]
        while nn>dd:
            v = pre[s[1]]
            nn = dis[v]
        d.append(v)
        dis2 = [-1]*N
        dis2[v] = 0
        while d:
            ver = d.popleft()
            for nex in G[ver]:
                if dis2[nex]==-1:
                    dis2[nex]=dis2[ver]+1
                    d.append(nex)
        for ss in s:
            if dis2[ss]!=nn:
                print('No')
                exit()
        print('Yes')
0