結果

問題 No.1424 Ultrapalindrome
ユーザー AEnAEn
提出日時 2022-11-30 23:15:16
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,144 bytes
コンパイル時間 388 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 99,864 KB
最終ジャッジ日時 2024-04-16 18:45:55
合計ジャッジ時間 6,260 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
54,016 KB
testcase_01 AC 46 ms
53,760 KB
testcase_02 AC 47 ms
53,632 KB
testcase_03 AC 47 ms
53,888 KB
testcase_04 AC 47 ms
53,760 KB
testcase_05 AC 47 ms
53,888 KB
testcase_06 AC 46 ms
53,632 KB
testcase_07 AC 47 ms
53,760 KB
testcase_08 WA -
testcase_09 AC 299 ms
89,856 KB
testcase_10 AC 290 ms
90,112 KB
testcase_11 AC 232 ms
85,760 KB
testcase_12 AC 230 ms
87,808 KB
testcase_13 AC 143 ms
78,976 KB
testcase_14 AC 205 ms
86,144 KB
testcase_15 AC 50 ms
55,040 KB
testcase_16 AC 176 ms
83,200 KB
testcase_17 AC 206 ms
85,888 KB
testcase_18 AC 191 ms
82,816 KB
testcase_19 AC 235 ms
85,504 KB
testcase_20 AC 134 ms
78,336 KB
testcase_21 AC 200 ms
83,072 KB
testcase_22 AC 167 ms
81,280 KB
testcase_23 AC 108 ms
77,952 KB
testcase_24 AC 127 ms
78,080 KB
testcase_25 AC 125 ms
78,080 KB
testcase_26 AC 272 ms
87,168 KB
testcase_27 WA -
testcase_28 AC 161 ms
87,552 KB
testcase_29 WA -
testcase_30 AC 194 ms
98,580 KB
testcase_31 AC 194 ms
99,864 KB
権限があれば一括ダウンロードができます

ソースコード

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[v]
            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