結果

問題 No.1424 Ultrapalindrome
ユーザー hir355hir355
提出日時 2021-03-12 21:54:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 304 ms / 2,000 ms
コード長 1,203 bytes
コンパイル時間 381 ms
コンパイル使用メモリ 87,092 KB
実行使用メモリ 114,184 KB
最終ジャッジ日時 2023-08-04 19:01:02
合計ジャッジ時間 7,500 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,352 KB
testcase_01 AC 71 ms
71,176 KB
testcase_02 AC 71 ms
71,004 KB
testcase_03 AC 71 ms
71,152 KB
testcase_04 AC 71 ms
71,312 KB
testcase_05 AC 71 ms
71,344 KB
testcase_06 AC 72 ms
71,156 KB
testcase_07 AC 71 ms
71,356 KB
testcase_08 AC 71 ms
71,452 KB
testcase_09 AC 248 ms
88,840 KB
testcase_10 AC 245 ms
88,596 KB
testcase_11 AC 185 ms
85,396 KB
testcase_12 AC 216 ms
87,428 KB
testcase_13 AC 151 ms
80,896 KB
testcase_14 AC 205 ms
86,728 KB
testcase_15 AC 74 ms
71,236 KB
testcase_16 AC 197 ms
83,508 KB
testcase_17 AC 191 ms
85,768 KB
testcase_18 AC 173 ms
82,952 KB
testcase_19 AC 204 ms
85,820 KB
testcase_20 AC 137 ms
78,792 KB
testcase_21 AC 186 ms
84,160 KB
testcase_22 AC 164 ms
82,136 KB
testcase_23 AC 121 ms
78,576 KB
testcase_24 AC 135 ms
78,796 KB
testcase_25 AC 138 ms
78,816 KB
testcase_26 AC 230 ms
87,660 KB
testcase_27 AC 304 ms
101,144 KB
testcase_28 AC 186 ms
90,968 KB
testcase_29 AC 196 ms
91,948 KB
testcase_30 AC 210 ms
114,184 KB
testcase_31 AC 204 ms
106,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
g = [[] for _ in range(n)]
vi = [0] * n
for _ in range(n - 1):
    a, b = map(int, input().split())
    vi[a - 1] += 1
    vi[b - 1] += 1
    g[a - 1].append(b - 1)
    g[b - 1].append(a - 1)
s = [0]
d = [-1] * n
d[0] = 0
while s:
    p = s.pop()
    for node in g[p]:
        if d[node] == -1:
            d[node] = d[p] + 1
            s.append(node)
k = d.index(max(d))
s = [k]
d = [-1] * n
d[k] = 0
while s:
    p = s.pop()
    for node in g[p]:
        if d[node] == -1:
            d[node] = d[p] + 1
            s.append(node)
r = max(d)
if r % 2 == 1:
    for i in range(n):
        if vi[i] >= 3:
            print('No')
            break
    else:
        print('Yes')
else:
    c = d.index(r // 2)
    if d.count(c) >= 2:
        print('No')
    else:
        s = [c]
        d = [-1] * n
        d[c] = 0
        while s:
            p = s.pop()
            for node in g[p]:
                if d[node] == -1:
                    d[node] = d[p] + 1
                    s.append(node)
        for i in range(n):
            if (i != c and vi[i] >= 3) or (vi[i] == 1 and d[i] != r // 2):
                print('No')
                break
        else:
            print('Yes')
0