結果

問題 No.2202 贅沢てりたまチキン
ユーザー FromBooskaFromBooska
提出日時 2023-03-05 16:58:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 656 ms / 2,000 ms
コード長 1,569 bytes
コンパイル時間 166 ms
コンパイル使用メモリ 81,708 KB
実行使用メモリ 82,928 KB
最終ジャッジ日時 2023-10-18 04:56:43
合計ジャッジ時間 5,181 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,520 KB
testcase_01 AC 39 ms
53,520 KB
testcase_02 AC 39 ms
53,520 KB
testcase_03 AC 39 ms
53,520 KB
testcase_04 AC 39 ms
53,520 KB
testcase_05 AC 39 ms
53,520 KB
testcase_06 AC 39 ms
53,520 KB
testcase_07 AC 39 ms
53,520 KB
testcase_08 AC 39 ms
53,520 KB
testcase_09 AC 40 ms
53,520 KB
testcase_10 AC 47 ms
62,892 KB
testcase_11 AC 39 ms
53,520 KB
testcase_12 AC 40 ms
53,520 KB
testcase_13 AC 40 ms
53,520 KB
testcase_14 AC 172 ms
79,060 KB
testcase_15 AC 170 ms
79,060 KB
testcase_16 AC 168 ms
79,052 KB
testcase_17 AC 169 ms
79,188 KB
testcase_18 AC 128 ms
80,376 KB
testcase_19 AC 126 ms
79,016 KB
testcase_20 AC 191 ms
79,268 KB
testcase_21 AC 190 ms
79,264 KB
testcase_22 AC 170 ms
75,960 KB
testcase_23 AC 285 ms
77,532 KB
testcase_24 AC 219 ms
76,700 KB
testcase_25 AC 656 ms
82,928 KB
testcase_26 AC 177 ms
79,228 KB
testcase_27 AC 172 ms
79,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 頂点倍化
# BFS TLEしたのでUnion Findにする

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(N*2+1)
for i in range(M):
    a, b = map(int, input().split())
    UF.union(a, b+N)
    UF.union(b, a+N)
    
ans = 'Yes'
for i in range(1, N+1):
    if UF.same(i, i+N) == False:
        ans = 'No'
print(ans)
0