結果

問題 No.408 五輪ピック
ユーザー lam6er
提出日時 2025-04-16 16:39:22
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,261 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 82,108 KB
実行使用メモリ 176,772 KB
最終ジャッジ日時 2025-04-16 16:41:06
合計ジャッジ時間 8,822 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23 WA * 1 MLE * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from sys import stdin

def main():
    sys.setrecursionlimit(1 << 25)
    N, M = map(int, stdin.readline().split())
    adj = [set() for _ in range(N + 1)]
    for _ in range(M):
        u, v = map(int, stdin.readline().split())
        adj[u].add(v)
        adj[v].add(u)
    
    S = adj[1]
    if len(S) < 2:
        print("NO")
        return
    
    # Precompute neighbors_in_S for each node
    neighbors_in_S = [set() for _ in range(N + 1)]
    for node in S:
        for neighbor in adj[node]:
            neighbors_in_S[neighbor].add(node)
    
    for a in S:
        two_step = set()
        # Iterate over neighbors of a, excluding 1 and a itself
        for b in adj[a]:
            if b == 1 or b == a:
                continue
            # Iterate over neighbors of b, excluding 1, a, and b
            for c in adj[b]:
                if c == 1 or c == a or c == b:
                    continue
                two_step.add(c)
        
        # Check each c in two_step
        for c in two_step:
            s = neighbors_in_S[c]
            if not s:
                continue
            if a not in s or len(s) >= 2:
                print("YES")
                return
    
    print("NO")

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