結果

問題 No.2202 贅沢てりたまチキン
ユーザー minatominato
提出日時 2023-03-09 06:47:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 471 ms / 2,000 ms
コード長 1,032 bytes
コンパイル時間 266 ms
コンパイル使用メモリ 81,840 KB
実行使用メモリ 81,948 KB
最終ジャッジ日時 2023-10-18 06:11:50
合計ジャッジ時間 5,125 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,624 KB
testcase_01 AC 39 ms
53,624 KB
testcase_02 AC 38 ms
53,624 KB
testcase_03 AC 40 ms
53,624 KB
testcase_04 AC 40 ms
53,624 KB
testcase_05 AC 38 ms
53,624 KB
testcase_06 AC 37 ms
53,624 KB
testcase_07 AC 37 ms
53,624 KB
testcase_08 AC 37 ms
53,624 KB
testcase_09 AC 38 ms
53,624 KB
testcase_10 AC 39 ms
56,752 KB
testcase_11 AC 38 ms
53,624 KB
testcase_12 AC 38 ms
53,624 KB
testcase_13 AC 38 ms
53,624 KB
testcase_14 AC 164 ms
79,132 KB
testcase_15 AC 164 ms
79,136 KB
testcase_16 AC 158 ms
79,108 KB
testcase_17 AC 165 ms
79,284 KB
testcase_18 AC 123 ms
80,496 KB
testcase_19 AC 118 ms
79,108 KB
testcase_20 AC 193 ms
79,424 KB
testcase_21 AC 195 ms
79,420 KB
testcase_22 AC 169 ms
76,064 KB
testcase_23 AC 257 ms
77,452 KB
testcase_24 AC 213 ms
76,816 KB
testcase_25 AC 471 ms
81,948 KB
testcase_26 AC 174 ms
79,324 KB
testcase_27 AC 164 ms
79,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parent_or_size = [-1] * n

    def merge(self, a, b):
        x = self.root(a)
        y = self.root(b)
        if x == y:
            return False
        if -self.parent_or_size[x] < -self.parent_or_size[y]:
            x, y = y, x
        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x
        return True

    def same(self, a, b):
        return self.root(a) == self.root(b)

    def root(self, a):
        if self.parent_or_size[a] < 0:
            return a
        self.parent_or_size[a] = self.root(self.parent_or_size[a])
        return self.parent_or_size[a]

    def size(self, a):
        return -self.parent_or_size[self.root(a)]


N, M = map(int, input().split())

uf = UnionFind(N * 2)

for _ in range(M):
    a, b = map(int, input().split())
    uf.merge(a - 1, b - 1 + N)
    uf.merge(a - 1 + N, b - 1)

for i in range(N):
    if not uf.same(i, i + N):
        print("No")
        exit()

print("Yes")
0