結果

問題 No.1424 Ultrapalindrome
ユーザー hir355hir355
提出日時 2021-03-12 21:54:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 297 ms / 2,000 ms
コード長 1,203 bytes
コンパイル時間 922 ms
コンパイル使用メモリ 82,072 KB
実行使用メモリ 106,420 KB
最終ジャッジ日時 2024-04-22 17:05:40
合計ジャッジ時間 5,495 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,964 KB
testcase_01 AC 36 ms
53,200 KB
testcase_02 AC 38 ms
53,648 KB
testcase_03 AC 37 ms
54,172 KB
testcase_04 AC 38 ms
52,256 KB
testcase_05 AC 36 ms
53,220 KB
testcase_06 AC 38 ms
53,292 KB
testcase_07 AC 37 ms
52,544 KB
testcase_08 AC 37 ms
53,552 KB
testcase_09 AC 231 ms
87,520 KB
testcase_10 AC 242 ms
87,552 KB
testcase_11 AC 164 ms
83,396 KB
testcase_12 AC 193 ms
86,380 KB
testcase_13 AC 114 ms
79,044 KB
testcase_14 AC 184 ms
85,800 KB
testcase_15 AC 40 ms
53,972 KB
testcase_16 AC 151 ms
82,876 KB
testcase_17 AC 159 ms
83,424 KB
testcase_18 AC 145 ms
82,180 KB
testcase_19 AC 183 ms
85,012 KB
testcase_20 AC 106 ms
77,940 KB
testcase_21 AC 162 ms
83,160 KB
testcase_22 AC 136 ms
81,436 KB
testcase_23 AC 90 ms
77,644 KB
testcase_24 AC 104 ms
78,012 KB
testcase_25 AC 106 ms
77,840 KB
testcase_26 AC 213 ms
86,612 KB
testcase_27 AC 297 ms
100,396 KB
testcase_28 AC 153 ms
89,844 KB
testcase_29 AC 162 ms
90,684 KB
testcase_30 AC 178 ms
106,420 KB
testcase_31 AC 173 ms
105,804 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