結果

問題 No.583 鉄道同好会
ユーザー tktk_snsntktk_snsn
提出日時 2021-01-14 00:38:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 1,412 bytes
コンパイル時間 313 ms
コンパイル使用メモリ 82,180 KB
実行使用メモリ 75,836 KB
最終ジャッジ日時 2024-05-02 08:28:28
合計ジャッジ時間 2,309 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
52,412 KB
testcase_01 AC 36 ms
52,272 KB
testcase_02 AC 34 ms
53,024 KB
testcase_03 AC 32 ms
53,436 KB
testcase_04 AC 32 ms
54,024 KB
testcase_05 AC 32 ms
53,036 KB
testcase_06 AC 32 ms
53,092 KB
testcase_07 AC 33 ms
52,624 KB
testcase_08 AC 34 ms
52,208 KB
testcase_09 AC 37 ms
52,976 KB
testcase_10 AC 36 ms
58,940 KB
testcase_11 AC 52 ms
71,760 KB
testcase_12 AC 53 ms
73,816 KB
testcase_13 AC 53 ms
73,100 KB
testcase_14 AC 53 ms
74,148 KB
testcase_15 AC 62 ms
75,648 KB
testcase_16 AC 65 ms
75,512 KB
testcase_17 AC 69 ms
75,836 KB
testcase_18 AC 67 ms
75,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)
        self.rank = [0] * (n + 1)

    def find(self, x):
        if self.root[x] < 0:
            return x
        self.root[x] = self.find(self.root[x])
        return self.root[x]

    def isSame(self, x, y):
        return self.find(x) == self.find(y)

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        return True

    def size(self, x):
        return -self.root[self.find(x)]


N, M = map(int, input().split())
uf = UF_tree(N)
deg = [0] * N
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    uf.unite(a, b)
    deg[a] += 1
    deg[b] += 1

leader = set()
for i in range(N):
    if uf.size(i) > 1:
        leader.add(uf.find(i))

if len(leader) > 1:
    print("NO")
    exit()

odd = 0
even = 0
for d in deg:
    if not d:
        continue
    if d % 2 == 0:
        even += 1
    else:
        odd += 1

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