結果
| 問題 | No.583 鉄道同好会 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-10-01 09:45:19 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,832 ms / 2,000 ms | 
| コード長 | 714 bytes | 
| コンパイル時間 | 101 ms | 
| コンパイル使用メモリ | 12,672 KB | 
| 実行使用メモリ | 120,300 KB | 
| 最終ジャッジ日時 | 2024-07-07 01:10:26 | 
| 合計ジャッジ時間 | 22,598 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
| 外部呼び出し有り | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 16 | 
ソースコード
from collections import Counter
from scipy.sparse.csgraph import connected_components
from scipy.sparse import csr_matrix
N, M = map(int, input().split())
deg = [0] * N
valid_stations = set()
frm, to = [], []
for _ in range(M):
    a, b = map(int, input().split())
    valid_stations.add(a)
    valid_stations.add(b)
    frm.append(a)
    to.append(b)
    deg[a] += 1
    deg[b] += 1
# 連結判定
matr = csr_matrix(([1] * M, (frm, to)), shape=(N, N))
_, labels = connected_components(matr)
ct = Counter(labels[list(valid_stations)])
if len(ct) > 1:
    print('NO')
    exit()
# 一筆書き可能性判定
is_odd = 0
for x in deg:
    if x % 2:
        is_odd += 1
print('YES' if is_odd in (0, 2) else 'NO')
            
            
            
        