結果

問題 No.2202 贅沢てりたまチキン
ユーザー noriocnorioc
提出日時 2023-02-03 23:27:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 563 ms / 2,000 ms
コード長 1,417 bytes
コンパイル時間 314 ms
コンパイル使用メモリ 87,060 KB
実行使用メモリ 91,376 KB
最終ジャッジ日時 2023-09-15 19:44:56
合計ジャッジ時間 6,449 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,288 KB
testcase_01 AC 70 ms
70,976 KB
testcase_02 AC 72 ms
70,772 KB
testcase_03 AC 70 ms
70,904 KB
testcase_04 AC 71 ms
71,032 KB
testcase_05 AC 70 ms
70,900 KB
testcase_06 AC 70 ms
70,860 KB
testcase_07 AC 71 ms
71,112 KB
testcase_08 AC 71 ms
71,204 KB
testcase_09 AC 71 ms
71,140 KB
testcase_10 AC 79 ms
84,460 KB
testcase_11 AC 72 ms
70,904 KB
testcase_12 AC 71 ms
71,148 KB
testcase_13 AC 71 ms
71,288 KB
testcase_14 AC 202 ms
87,852 KB
testcase_15 AC 199 ms
87,848 KB
testcase_16 AC 191 ms
86,468 KB
testcase_17 AC 201 ms
86,648 KB
testcase_18 AC 157 ms
88,768 KB
testcase_19 AC 154 ms
86,572 KB
testcase_20 AC 236 ms
87,912 KB
testcase_21 AC 233 ms
88,024 KB
testcase_22 AC 192 ms
77,472 KB
testcase_23 AC 292 ms
80,252 KB
testcase_24 AC 244 ms
79,300 KB
testcase_25 AC 563 ms
91,376 KB
testcase_26 AC 217 ms
88,096 KB
testcase_27 AC 201 ms
87,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.data = [-1] * (n + 1)
        self.nexts = [-1] * (n + 1) # 次の要素(なければ-1)
        self.tails = list(range(n + 1)) # 末尾の要素

    def root(self, a: int) -> int:
        if self.data[a] < 0: return a
        self.data[a] = self.root(self.data[a])
        return self.data[a]

    def unite(self, a: int, b: int) -> bool:
        pa = self.root(a)
        pb = self.root(b)
        if pa == pb: return False
        if self.data[pa] > self.data[pb]:
            pa, pb = pb, pa
        self.data[pa] += self.data[pb] # pa を pb をつなげる
        self.data[pb] = pa
        # pa の末尾に pb を繋げる
        self.nexts[self.tails[pa]] = pb
        self.tails[pa] = self.tails[pb]
        return True

    def issame(self, a: int, b: int) -> bool:
        return self.root(a) == self.root(b)

    def size(self, a: int) -> int:
        """a が属する集合のサイズ"""
        return -self.data[self.root(a)]

    def group(self, a):
        """a が属する集合"""
        v = self.root(a)
        while v != -1:
            yield v
            v = self.nexts[v]


N, M = map(int, input().split())
uf = UnionFind(N * 2 + 10)
for _ in range(M):
    A, B = map(int, input().split())
    uf.unite(A, B + N)
    uf.unite(A + N, B)

if all(uf.issame(i, i+N) for i in range(1, N+1)):
    print('Yes')
else:
    print('No')
0