結果

問題 No.583 鉄道同好会
ユーザー lam6er
提出日時 2025-03-20 20:21:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 1,338 bytes
コンパイル時間 166 ms
コンパイル使用メモリ 82,904 KB
実行使用メモリ 80,116 KB
最終ジャッジ日時 2025-03-20 20:22:35
合計ジャッジ時間 2,682 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    N, M = map(int, sys.stdin.readline().split())
    adj = [[] for _ in range(N)]
    degree = [0] * N

    for _ in range(M):
        Sa, Sb = map(int, sys.stdin.readline().split())
        adj[Sa].append(Sb)
        adj[Sb].append(Sa)
        degree[Sa] += 1
        degree[Sb] += 1

    # Check connectivity using BFS
    visited = [False] * N
    start = None
    for i in range(N):
        if degree[i] > 0:
            start = i
            break
    if start is None:
        # All nodes are isolated, but M >=1 so this can't happen
        print("NO")
        return

    q = deque()
    q.append(start)
    visited[start] = True

    while q:
        u = q.popleft()
        for v in adj[u]:
            if not visited[v]:
                visited[v] = True
                q.append(v)

    # Check if all nodes with edges are visited
    connected = True
    for i in range(N):
        if degree[i] > 0 and not visited[i]:
            connected = False
            break

    if not connected:
        print("NO")
        return

    # Count odd degrees
    odd_count = 0
    for d in degree:
        if d % 2 != 0:
            odd_count += 1

    if odd_count == 0 or odd_count == 2:
        print("YES")
    else:
        print("NO")

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