結果

問題 No.2202 贅沢てりたまチキン
ユーザー noriocnorioc
提出日時 2023-02-03 23:26:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 560 ms / 2,000 ms
コード長 1,440 bytes
コンパイル時間 326 ms
コンパイル使用メモリ 86,516 KB
実行使用メモリ 91,084 KB
最終ジャッジ日時 2023-09-15 19:44:10
合計ジャッジ時間 6,635 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,020 KB
testcase_01 AC 72 ms
70,912 KB
testcase_02 AC 71 ms
70,828 KB
testcase_03 AC 71 ms
70,868 KB
testcase_04 AC 71 ms
71,208 KB
testcase_05 AC 69 ms
71,044 KB
testcase_06 AC 71 ms
71,188 KB
testcase_07 AC 70 ms
70,944 KB
testcase_08 AC 70 ms
70,992 KB
testcase_09 AC 70 ms
70,860 KB
testcase_10 AC 78 ms
84,128 KB
testcase_11 AC 69 ms
70,932 KB
testcase_12 AC 71 ms
70,872 KB
testcase_13 AC 71 ms
70,848 KB
testcase_14 AC 210 ms
87,856 KB
testcase_15 AC 198 ms
87,848 KB
testcase_16 AC 190 ms
86,612 KB
testcase_17 AC 197 ms
86,632 KB
testcase_18 AC 157 ms
88,656 KB
testcase_19 AC 153 ms
86,640 KB
testcase_20 AC 224 ms
88,000 KB
testcase_21 AC 223 ms
88,020 KB
testcase_22 AC 194 ms
77,416 KB
testcase_23 AC 291 ms
80,528 KB
testcase_24 AC 240 ms
79,224 KB
testcase_25 AC 560 ms
91,084 KB
testcase_26 AC 208 ms
88,244 KB
testcase_27 AC 204 ms
87,860 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)


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