結果

問題 No.408 五輪ピック
ユーザー lam6er
提出日時 2025-04-16 16:40:47
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,170 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 81,944 KB
実行使用メモリ 94,008 KB
最終ジャッジ日時 2025-04-16 16:41:56
合計ジャッジ時間 3,340 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15 WA * 2 MLE * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict

def main():
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    M = int(input[idx])
    idx += 1

    adj = defaultdict(list)
    for _ in range(M):
        u = int(input[idx])
        idx += 1
        v = int(input[idx])
        idx += 1
        adj[u].append(v)
        adj[v].append(u)

    # Collect neighbors of 1
    S = set(adj[1])
    if len(S) < 2:
        print("NO")
        return

    # Precompute for each node, the set of neighbors in S
    s_neighbors = defaultdict(set)
    for node in adj:
        for neighbor in adj[node]:
            if neighbor in S:
                s_neighbors[node].add(neighbor)

    # Check all nodes except 1
    for x in range(1, N+1):
        if x == 1:
            continue
        d_set = s_neighbors[x]
        if not d_set:
            continue
        for y in adj[x]:
            a_set = s_neighbors[y]
            if not a_set:
                continue
            for a in a_set:
                if a not in d_set:
                    print("YES")
                    return

    print("NO")

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