結果

問題 No.2202 贅沢てりたまチキン
ユーザー kohakoha11301kohakoha11301
提出日時 2023-03-01 11:45:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 459 ms / 2,000 ms
コード長 1,631 bytes
コンパイル時間 1,503 ms
コンパイル使用メモリ 86,668 KB
実行使用メモリ 86,168 KB
最終ジャッジ日時 2023-10-14 16:28:47
合計ジャッジ時間 10,367 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,268 KB
testcase_01 AC 77 ms
71,548 KB
testcase_02 AC 80 ms
71,692 KB
testcase_03 AC 84 ms
71,268 KB
testcase_04 AC 82 ms
71,444 KB
testcase_05 AC 78 ms
71,480 KB
testcase_06 AC 77 ms
71,372 KB
testcase_07 AC 85 ms
71,516 KB
testcase_08 AC 87 ms
71,708 KB
testcase_09 AC 79 ms
71,672 KB
testcase_10 AC 93 ms
79,996 KB
testcase_11 AC 78 ms
71,460 KB
testcase_12 AC 80 ms
71,624 KB
testcase_13 AC 81 ms
71,656 KB
testcase_14 AC 191 ms
80,924 KB
testcase_15 AC 194 ms
80,924 KB
testcase_16 AC 185 ms
80,956 KB
testcase_17 AC 190 ms
80,992 KB
testcase_18 AC 149 ms
79,352 KB
testcase_19 AC 156 ms
80,920 KB
testcase_20 AC 215 ms
81,268 KB
testcase_21 AC 212 ms
81,336 KB
testcase_22 AC 188 ms
77,944 KB
testcase_23 AC 271 ms
79,708 KB
testcase_24 AC 229 ms
78,320 KB
testcase_25 AC 459 ms
86,168 KB
testcase_26 AC 209 ms
81,184 KB
testcase_27 AC 200 ms
81,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


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

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(
            f'{r}: {m}' for r, m in self.all_group_members().items())


n, m = map(int, input().split())
uf = UnionFind(2 * n)
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    uf.union(2 * a + 1, 2 * b)
    uf.union(2 * a, 2 * b + 1)
cnt = 0
for i in range(n):
    if uf.same(2 * i, 2 * i + 1):
        cnt += 1
if cnt==n:
    print('Yes')
else:
    print('No')
0