結果

問題 No.1424 Ultrapalindrome
ユーザー GER_chenGER_chen
提出日時 2021-03-12 21:56:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 292 ms / 2,000 ms
コード長 1,134 bytes
コンパイル時間 444 ms
コンパイル使用メモリ 82,068 KB
実行使用メモリ 125,600 KB
最終ジャッジ日時 2024-04-22 17:06:19
合計ジャッジ時間 5,561 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,224 KB
testcase_01 AC 36 ms
51,968 KB
testcase_02 AC 36 ms
51,968 KB
testcase_03 AC 36 ms
52,096 KB
testcase_04 AC 36 ms
51,968 KB
testcase_05 AC 35 ms
52,352 KB
testcase_06 AC 37 ms
52,224 KB
testcase_07 AC 37 ms
51,968 KB
testcase_08 AC 37 ms
52,480 KB
testcase_09 AC 188 ms
101,696 KB
testcase_10 AC 191 ms
101,360 KB
testcase_11 AC 145 ms
93,312 KB
testcase_12 AC 180 ms
101,056 KB
testcase_13 AC 93 ms
81,120 KB
testcase_14 AC 153 ms
93,900 KB
testcase_15 AC 37 ms
52,752 KB
testcase_16 AC 136 ms
91,596 KB
testcase_17 AC 150 ms
93,320 KB
testcase_18 AC 186 ms
98,316 KB
testcase_19 AC 261 ms
104,808 KB
testcase_20 AC 115 ms
83,328 KB
testcase_21 AC 205 ms
98,972 KB
testcase_22 AC 163 ms
90,496 KB
testcase_23 AC 58 ms
72,064 KB
testcase_24 AC 111 ms
81,152 KB
testcase_25 AC 104 ms
80,896 KB
testcase_26 AC 292 ms
110,216 KB
testcase_27 AC 225 ms
115,636 KB
testcase_28 AC 122 ms
98,340 KB
testcase_29 AC 133 ms
113,408 KB
testcase_30 AC 163 ms
125,600 KB
testcase_31 AC 141 ms
111,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys;input = lambda: sys.stdin.readline().rstrip()
n = int(input())
Adj = [set() for _ in range(n)]
Leaves = set()
for i in range(n-1):
    a, b = map(int,input().split())
    a -= 1
    b -= 1
    Adj[a].add(b)
    Adj[b].add(a)
    if a not in Leaves:
        if len(Adj[a]) == 1:
            Leaves.add(a)
    else:
        Leaves.remove(a)
    if b not in Leaves:
        if len(Adj[b]) == 1:
            Leaves.add(b)
    else:
        Leaves.remove(b)

n_l = len(Leaves)
if n_l == 2:
    print('Yes')
    exit()
if n%n_l != 1:
    print('No')
    exit()

ans = 'Yes'
Vertices = set(range(n))
while Vertices and n > 2:
    nxtLeaves = set()
    Candidates = set()
    for l in Leaves:
        Vertices.remove(l)
        for x in Adj[l]:
            Candidates.add(x)
            if len(Adj[x]) == 2:
                nxtLeaves.add(x)
    if nxtLeaves == set() and len(Candidates) == 1:
        break
    elif len(nxtLeaves) == n_l:
        for nl in nxtLeaves:
            Adj[nl] = [a for a in Adj[nl] if a not in Leaves]
        Leaves = nxtLeaves
    else:
        ans = 'No'
        break
    n -= len(Leaves)
print(ans)
0