結果

問題 No.2202 贅沢てりたまチキン
ユーザー helloprahellopra
提出日時 2023-02-10 23:45:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 579 ms / 2,000 ms
コード長 2,358 bytes
コンパイル時間 395 ms
コンパイル使用メモリ 86,920 KB
実行使用メモリ 175,216 KB
最終ジャッジ日時 2023-09-22 00:40:16
合計ジャッジ時間 8,926 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,360 KB
testcase_01 AC 94 ms
71,884 KB
testcase_02 AC 95 ms
71,800 KB
testcase_03 AC 96 ms
71,348 KB
testcase_04 AC 93 ms
71,736 KB
testcase_05 AC 93 ms
71,568 KB
testcase_06 AC 94 ms
71,684 KB
testcase_07 AC 97 ms
71,784 KB
testcase_08 AC 93 ms
71,864 KB
testcase_09 AC 93 ms
71,792 KB
testcase_10 AC 197 ms
141,708 KB
testcase_11 AC 99 ms
71,688 KB
testcase_12 AC 94 ms
71,728 KB
testcase_13 AC 95 ms
71,708 KB
testcase_14 AC 266 ms
139,376 KB
testcase_15 AC 265 ms
139,048 KB
testcase_16 AC 260 ms
139,360 KB
testcase_17 AC 300 ms
175,216 KB
testcase_18 AC 197 ms
106,008 KB
testcase_19 AC 233 ms
110,500 KB
testcase_20 AC 295 ms
97,996 KB
testcase_21 AC 294 ms
97,800 KB
testcase_22 AC 212 ms
78,128 KB
testcase_23 AC 303 ms
81,284 KB
testcase_24 AC 261 ms
78,824 KB
testcase_25 AC 579 ms
167,392 KB
testcase_26 AC 305 ms
171,452 KB
testcase_27 AC 257 ms
120,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

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

    def find(self, x):
        #要素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が属するグループと要素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):
        #要素xが属するグループのサイズ(要素数)を返す
        return -self.parents[self.find(x)]

    def same(self, x, y):
        #要素x, yが同じグループに属するかどうかを返す
        return self.find(x) == self.find(y)

    def members(self, x):
        #要素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):
        #{ルート要素: [そのグループに含まれる要素のリスト], ...}のdefaultdictを返す
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        #print()での表示用 #ルート要素: [そのグループに含まれる要素のリスト]を文字列で返す
        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 i in range(M):
	a,b = map(int,input().split())
	a-=1
	b-=1
	uf.union(a+N,b)
	uf.union(a,b+N)

for k,l in uf.all_group_members().items():
	l = set(l)
	while l:
		t = l.pop()
		if t < N: x = t+N
		else:x=t-N

		if x in l:
			l.discard(x)
			continue
		else:
			print("No")
			exit()

print("Yes")
0