結果

問題 No.583 鉄道同好会
ユーザー mitsuomitsuo
提出日時 2017-12-31 17:24:51
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 537 ms / 2,000 ms
コード長 1,017 bytes
コンパイル時間 101 ms
コンパイル使用メモリ 10,884 KB
実行使用メモリ 28,900 KB
最終ジャッジ日時 2023-08-23 12:32:16
合計ジャッジ時間 3,715 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,720 KB
testcase_01 AC 18 ms
8,648 KB
testcase_02 AC 19 ms
8,648 KB
testcase_03 AC 18 ms
8,412 KB
testcase_04 AC 19 ms
8,504 KB
testcase_05 AC 19 ms
8,492 KB
testcase_06 AC 19 ms
8,600 KB
testcase_07 AC 19 ms
8,520 KB
testcase_08 AC 19 ms
8,636 KB
testcase_09 AC 19 ms
8,728 KB
testcase_10 AC 22 ms
8,576 KB
testcase_11 AC 139 ms
13,388 KB
testcase_12 AC 185 ms
15,208 KB
testcase_13 AC 184 ms
15,180 KB
testcase_14 AC 185 ms
15,248 KB
testcase_15 AC 234 ms
17,352 KB
testcase_16 AC 437 ms
25,264 KB
testcase_17 AC 527 ms
28,900 KB
testcase_18 AC 537 ms
28,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections

def solve(N, M, data):

    count = collections.Counter()
    unionset = []
    for d in data:
        count.update(d)
        d0s = None
        d1s = None
        for s in unionset:
            if d[0] in s:
                d0s = s
            elif d[1] in s:
                d1s = s
        if d0s == None and d1s == None:
            unionset.append(set([d[0], d[1]]))
        elif d0s != None and d1s == None:
            d0s.add(d[1])
        elif d0s == None and d1s != None:
            d1s.add(d[0])
        else:
            unionset.remove(d0s)
            unionset.remove(d1s)
            n = d0s.union(d1s)
            unionset.append(n)

    oddcount = 0
    for val in count.values():
        if val % 2 == 1:
            oddcount += 1

    return "YES" if oddcount <= 2 and len(unionset) == 1 else "NO"


if __name__ == '__main__':
    N, M = map(int, input().split())
    d = []
    for _ in range(0, M):
        d.append(list(map(int, input().split())))
    print(solve(N, M, d))
0