結果

問題 No.1424 Ultrapalindrome
ユーザー KudeKude
提出日時 2021-03-12 21:44:38
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 533 ms / 2,000 ms
コード長 837 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 49,920 KB
最終ジャッジ日時 2024-04-22 17:01:34
合計ジャッジ時間 8,458 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,880 KB
testcase_01 AC 30 ms
10,880 KB
testcase_02 AC 33 ms
10,880 KB
testcase_03 AC 33 ms
10,880 KB
testcase_04 AC 31 ms
11,008 KB
testcase_05 AC 30 ms
10,880 KB
testcase_06 AC 30 ms
10,752 KB
testcase_07 AC 30 ms
10,880 KB
testcase_08 AC 30 ms
10,880 KB
testcase_09 AC 406 ms
24,192 KB
testcase_10 AC 404 ms
24,448 KB
testcase_11 AC 282 ms
20,480 KB
testcase_12 AC 377 ms
23,424 KB
testcase_13 AC 119 ms
14,336 KB
testcase_14 AC 314 ms
21,376 KB
testcase_15 AC 30 ms
10,880 KB
testcase_16 AC 249 ms
19,200 KB
testcase_17 AC 304 ms
20,736 KB
testcase_18 AC 231 ms
17,792 KB
testcase_19 AC 361 ms
21,248 KB
testcase_20 AC 103 ms
13,440 KB
testcase_21 AC 309 ms
20,644 KB
testcase_22 AC 186 ms
16,128 KB
testcase_23 AC 62 ms
12,032 KB
testcase_24 AC 99 ms
13,568 KB
testcase_25 AC 91 ms
12,928 KB
testcase_26 AC 421 ms
23,296 KB
testcase_27 AC 533 ms
28,416 KB
testcase_28 AC 434 ms
28,032 KB
testcase_29 AC 525 ms
49,920 KB
testcase_30 AC 500 ms
26,496 KB
testcase_31 AC 498 ms
29,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 7)
n = int(input())
if n <= 3:
    print('Yes')
    exit()
deg = [0] * n
to = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
    deg[a] += 1
    deg[b] += 1
imx = max(range(n), key=lambda i: deg[i])
if deg[imx] == 2:
    print('Yes')
    exit()
if any(i != imx and deg[i] >= 3 for i in range(n)):
    print('No')
    exit()
depth = [0] * n
def dfs(u, p=-1):
    if p >= 0:
        depth[u] = depth[p] + 1
    for v in to[u]:
        if v == p:
            continue
        dfs(v, u)
dfs(imx)
d = -1
ok = True
for i in range(n):
    if deg[i] == 1:
        if d == -1:
            d = depth[i]
        elif d != depth[i]:
            ok = False
            break
ans = 'Yes' if ok else 'No'
print(ans)
0