結果

問題 No.1424 Ultrapalindrome
ユーザー ayaoniayaoni
提出日時 2021-03-13 05:12:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 1,321 bytes
コンパイル時間 479 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 99,328 KB
最終ジャッジ日時 2024-04-22 18:23:48
合計ジャッジ時間 4,588 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
54,400 KB
testcase_01 AC 42 ms
53,888 KB
testcase_02 AC 43 ms
54,144 KB
testcase_03 AC 42 ms
53,760 KB
testcase_04 AC 48 ms
54,016 KB
testcase_05 AC 42 ms
54,144 KB
testcase_06 AC 41 ms
53,888 KB
testcase_07 AC 43 ms
54,144 KB
testcase_08 AC 41 ms
53,760 KB
testcase_09 AC 122 ms
85,760 KB
testcase_10 AC 122 ms
86,016 KB
testcase_11 AC 109 ms
84,352 KB
testcase_12 AC 121 ms
85,248 KB
testcase_13 AC 87 ms
78,720 KB
testcase_14 AC 116 ms
84,352 KB
testcase_15 AC 47 ms
54,400 KB
testcase_16 AC 101 ms
81,024 KB
testcase_17 AC 109 ms
84,224 KB
testcase_18 AC 118 ms
82,304 KB
testcase_19 AC 138 ms
84,864 KB
testcase_20 AC 91 ms
78,848 KB
testcase_21 AC 122 ms
83,584 KB
testcase_22 AC 100 ms
80,640 KB
testcase_23 AC 71 ms
73,344 KB
testcase_24 AC 89 ms
78,208 KB
testcase_25 AC 91 ms
78,080 KB
testcase_26 AC 148 ms
86,656 KB
testcase_27 AC 151 ms
90,368 KB
testcase_28 AC 109 ms
89,600 KB
testcase_29 AC 117 ms
90,624 KB
testcase_30 AC 129 ms
99,072 KB
testcase_31 AC 137 ms
99,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


N = I()
Graph = [[] for _ in range(N+1)]
deg = [0]*(N+1)  # deg[i] = 頂点iの次数
for _ in range(N-1):
    a,b = MI()
    Graph[a].append(b)
    Graph[b].append(a)
    deg[a] += 1
    deg[b] += 1

count = [0]*N  # count[i] = 次数iの頂点数
for i in range(1,N+1):
    count[deg[i]] += 1

c = sum(count[3:])
if c == 0:
    print('Yes')
    exit()
if c >= 2:
    print('No')
    exit()

start = 0
for i in range(1,N+1):
    if deg[i] >= 3:
        start = i
        break

dist = [-1]*(N+1)
dist[start] = 0
deq = deque([start])
while deq:
    i = deq.pop()
    for j in Graph[i]:
        if dist[j] == -1:
            dist[j] = dist[i]+1
            deq.appendleft(j)

leaf = []
for i in range(1,N+1):
    if deg[i] == 1:
        leaf.append(i)

X = set([dist[i] for i in leaf])
if len(X) == 1:
    print('Yes')
else:
    print('No')
0