結果

問題 No.1424 Ultrapalindrome
ユーザー lam6er
提出日時 2025-03-31 17:40:41
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 811 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 82,704 KB
実行使用メモリ 111,900 KB
最終ジャッジ日時 2025-03-31 17:42:06
合計ジャッジ時間 4,087 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def main():
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    adj = [[] for _ in range(N + 1)]  # 1-based indexing

    for _ in range(N - 1):
        a = int(input[idx])
        b = int(input[idx + 1])
        adj[a].append(b)
        adj[b].append(a)
        idx += 2

    leaves = []
    for i in range(1, N + 1):
        if len(adj[i]) == 1:
            leaves.append(i)

    if len(leaves) == 2:
        print("Yes")
    else:
        # Collect the neighbors of all leaves
        neighbor_set = set()
        for leaf in leaves:
            neighbor_set.add(adj[leaf][0])  # since each leaf has exactly one neighbor

        if len(neighbor_set) == 1:
            print("Yes")
        else:
            print("No")

if __name__ == '__main__':
    main()
0