結果

問題 No.1424 Ultrapalindrome
ユーザー anagohirameanagohirame
提出日時 2021-03-12 22:05:37
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,101 bytes
コンパイル時間 156 ms
コンパイル使用メモリ 13,184 KB
実行使用メモリ 36,736 KB
最終ジャッジ日時 2024-04-22 13:04:18
合計ジャッジ時間 7,030 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
10,752 KB
testcase_01 AC 31 ms
10,752 KB
testcase_02 AC 31 ms
11,008 KB
testcase_03 AC 31 ms
10,880 KB
testcase_04 AC 31 ms
10,880 KB
testcase_05 AC 31 ms
10,880 KB
testcase_06 AC 32 ms
10,880 KB
testcase_07 WA -
testcase_08 AC 31 ms
10,752 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 187 ms
20,864 KB
testcase_12 AC 253 ms
24,448 KB
testcase_13 AC 86 ms
14,848 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 166 ms
19,712 KB
testcase_17 AC 197 ms
21,376 KB
testcase_18 AC 208 ms
18,816 KB
testcase_19 AC 295 ms
21,632 KB
testcase_20 AC 87 ms
13,568 KB
testcase_21 AC 240 ms
20,200 KB
testcase_22 AC 156 ms
16,256 KB
testcase_23 AC 60 ms
12,416 KB
testcase_24 AC 88 ms
13,440 KB
testcase_25 AC 79 ms
12,928 KB
testcase_26 AC 356 ms
23,424 KB
testcase_27 AC 533 ms
32,768 KB
testcase_28 AC 343 ms
31,360 KB
testcase_29 AC 448 ms
32,256 KB
testcase_30 AC 399 ms
33,792 KB
testcase_31 AC 422 ms
36,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
def input():
	return sys.stdin.buffer.readline()[:-1]

n = int(input())
adj = [[] for _ in range(n)]
for _ in range(n-1):
	a, b = map(int, input().split())
	adj[a-1].append(b-1)
	adj[b-1].append(a-1)

for i in range(n):
	if len(adj[i]) == 1:
		leaf = i
		break

depth = [1000000 for _ in range(n)]
depth[leaf] = 0
st = [(leaf, -1)]
while st:
	i, p = st.pop()
	if len(adj[i]) == 1 and i != leaf:
		other = i
		break
	for j in adj[i]:
		if j == p:
			continue
		else:
			depth[j] = depth[i] + 1
			st.append((j, i))

if depth[other] % 2 == 1:
	print("Yes" if max(len(adj[i]) for i in range(n)) else "No")
	sys.exit()

for i in range(n):
	if depth[i] == depth[other] // 2:
		center = i
		assumed = depth[i]
		break

depth = [1000000 for _ in range(n)]
depth[center] = 0
st = [(center, -1)]
while st:
	i, p = st.pop()
	if len(adj[i]) == 1 and depth[i] != assumed:
		print("No")
		break
	for j in adj[i]:
		if j == p:
			continue
		else:
			depth[j] = depth[i] + 1
			st.append((j, i))
else:
	for i in range(n):
		if i != center and len(adj[i]) > 2:
			print("No")
			break
	else:
		print("Yes")
0