結果

問題 No.583 鉄道同好会
ユーザー H3PO4H3PO4
提出日時 2020-10-01 09:45:19
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 695 ms / 2,000 ms
コード長 714 bytes
コンパイル時間 390 ms
コンパイル使用メモリ 10,912 KB
実行使用メモリ 87,328 KB
最終ジャッジ日時 2023-09-21 06:45:48
合計ジャッジ時間 11,256 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 695 ms
43,012 KB
testcase_01 AC 210 ms
43,276 KB
testcase_02 AC 213 ms
43,220 KB
testcase_03 AC 203 ms
42,876 KB
testcase_04 AC 205 ms
43,108 KB
testcase_05 AC 203 ms
42,992 KB
testcase_06 AC 212 ms
43,124 KB
testcase_07 AC 209 ms
43,120 KB
testcase_08 AC 206 ms
43,140 KB
testcase_09 AC 206 ms
43,136 KB
testcase_10 AC 210 ms
42,996 KB
testcase_11 AC 310 ms
44,468 KB
testcase_12 AC 346 ms
45,608 KB
testcase_13 AC 353 ms
45,340 KB
testcase_14 AC 349 ms
45,352 KB
testcase_15 AC 389 ms
47,000 KB
testcase_16 AC 551 ms
50,948 KB
testcase_17 AC 640 ms
52,608 KB
testcase_18 AC 633 ms
87,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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')
0