結果

問題 No.1424 Ultrapalindrome
ユーザー anagohirameanagohirame
提出日時 2021-03-12 23:23:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 1,123 bytes
コンパイル時間 339 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 105,164 KB
最終ジャッジ日時 2024-04-22 17:16:00
合計ジャッジ時間 4,488 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,224 KB
testcase_01 AC 37 ms
52,096 KB
testcase_02 AC 41 ms
52,352 KB
testcase_03 AC 37 ms
52,096 KB
testcase_04 AC 38 ms
52,352 KB
testcase_05 AC 36 ms
52,480 KB
testcase_06 AC 38 ms
52,352 KB
testcase_07 AC 38 ms
52,352 KB
testcase_08 AC 38 ms
52,480 KB
testcase_09 AC 106 ms
82,312 KB
testcase_10 AC 118 ms
82,304 KB
testcase_11 AC 94 ms
82,756 KB
testcase_12 AC 115 ms
82,816 KB
testcase_13 AC 70 ms
73,856 KB
testcase_14 AC 109 ms
81,664 KB
testcase_15 AC 42 ms
52,480 KB
testcase_16 AC 99 ms
81,792 KB
testcase_17 AC 95 ms
82,688 KB
testcase_18 AC 110 ms
81,024 KB
testcase_19 AC 124 ms
82,432 KB
testcase_20 AC 79 ms
77,780 KB
testcase_21 AC 124 ms
82,432 KB
testcase_22 AC 97 ms
80,512 KB
testcase_23 AC 73 ms
71,808 KB
testcase_24 AC 92 ms
78,464 KB
testcase_25 AC 91 ms
78,336 KB
testcase_26 AC 135 ms
85,248 KB
testcase_27 AC 169 ms
95,488 KB
testcase_28 AC 91 ms
86,720 KB
testcase_29 AC 123 ms
90,240 KB
testcase_30 AC 130 ms
102,888 KB
testcase_31 AC 133 ms
105,164 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