結果

問題 No.1424 Ultrapalindrome
ユーザー anagohirameanagohirame
提出日時 2021-03-12 23:24:18
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 493 ms / 2,000 ms
コード長 1,123 bytes
コンパイル時間 130 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 36,992 KB
最終ジャッジ日時 2024-04-22 17:16:08
合計ジャッジ時間 6,656 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
11,008 KB
testcase_01 AC 30 ms
11,136 KB
testcase_02 AC 31 ms
10,880 KB
testcase_03 AC 30 ms
11,008 KB
testcase_04 AC 33 ms
10,880 KB
testcase_05 AC 32 ms
11,008 KB
testcase_06 AC 30 ms
10,880 KB
testcase_07 AC 30 ms
11,008 KB
testcase_08 AC 31 ms
11,008 KB
testcase_09 AC 259 ms
24,448 KB
testcase_10 AC 256 ms
24,576 KB
testcase_11 AC 185 ms
20,864 KB
testcase_12 AC 247 ms
24,192 KB
testcase_13 AC 83 ms
14,592 KB
testcase_14 AC 199 ms
21,504 KB
testcase_15 AC 30 ms
11,008 KB
testcase_16 AC 163 ms
19,456 KB
testcase_17 AC 192 ms
21,504 KB
testcase_18 AC 197 ms
18,688 KB
testcase_19 AC 273 ms
21,632 KB
testcase_20 AC 83 ms
13,440 KB
testcase_21 AC 232 ms
20,072 KB
testcase_22 AC 152 ms
16,256 KB
testcase_23 AC 58 ms
12,416 KB
testcase_24 AC 83 ms
13,440 KB
testcase_25 AC 77 ms
13,056 KB
testcase_26 AC 339 ms
23,552 KB
testcase_27 AC 493 ms
32,896 KB
testcase_28 AC 251 ms
27,392 KB
testcase_29 AC 439 ms
32,172 KB
testcase_30 AC 394 ms
33,664 KB
testcase_31 AC 418 ms
36,992 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)

if max(len(adj[i]) for i in range(n)) <= 2:
	print("Yes")
	sys.exit()

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("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