結果

問題 No.1424 Ultrapalindrome
ユーザー tktk_snsntktk_snsn
提出日時 2021-03-15 14:57:38
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 276 ms / 2,000 ms
コード長 751 bytes
コンパイル時間 319 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 31,872 KB
最終ジャッジ日時 2024-04-24 18:04:47
合計ジャッジ時間 5,625 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
10,624 KB
testcase_01 AC 26 ms
10,624 KB
testcase_02 AC 27 ms
10,880 KB
testcase_03 AC 28 ms
10,880 KB
testcase_04 AC 28 ms
10,880 KB
testcase_05 AC 28 ms
10,752 KB
testcase_06 AC 26 ms
10,752 KB
testcase_07 AC 26 ms
11,008 KB
testcase_08 AC 26 ms
10,752 KB
testcase_09 AC 202 ms
24,192 KB
testcase_10 AC 194 ms
24,448 KB
testcase_11 AC 145 ms
20,480 KB
testcase_12 AC 184 ms
23,552 KB
testcase_13 AC 71 ms
14,336 KB
testcase_14 AC 159 ms
21,248 KB
testcase_15 AC 28 ms
10,880 KB
testcase_16 AC 129 ms
19,072 KB
testcase_17 AC 151 ms
20,864 KB
testcase_18 AC 133 ms
17,664 KB
testcase_19 AC 203 ms
21,120 KB
testcase_20 AC 64 ms
13,440 KB
testcase_21 AC 166 ms
20,608 KB
testcase_22 AC 119 ms
16,256 KB
testcase_23 AC 44 ms
11,904 KB
testcase_24 AC 65 ms
13,312 KB
testcase_25 AC 61 ms
12,800 KB
testcase_26 AC 245 ms
23,296 KB
testcase_27 AC 263 ms
28,416 KB
testcase_28 AC 216 ms
28,032 KB
testcase_29 AC 276 ms
31,872 KB
testcase_30 AC 249 ms
27,392 KB
testcase_31 AC 262 ms
30,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

N = int(input())
G = [[] for _ in range(N)]
deg = [0] * N
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)
    deg[a] += 1
    deg[b] += 1

leaf = deg.count(1)
branch = deg.count(2)
if leaf + branch == N:
    print("Yes")
    exit()
if N - leaf - branch > 1:
    print("No")
    exit()

root = deg.index(max(deg))
dep = [-1] * N
q = [root]
dep[root] = 0
L = set()
while q:
    s = q.pop()
    if deg[s] == 1:
        L.add(dep[s])
        continue
    for t in G[s]:
        if dep[t] == -1:
            dep[t] = dep[s] + 1
            q.append(t)

if len(L) == 1:
    print("Yes")
else:
    print("No")
0