結果

問題 No.2202 贅沢てりたまチキン
ユーザー nikoro_is_wolfnikoro_is_wolf
提出日時 2023-02-03 21:42:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 559 ms / 2,000 ms
コード長 1,456 bytes
コンパイル時間 377 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 88,020 KB
最終ジャッジ日時 2023-09-15 17:23:30
合計ジャッジ時間 6,732 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,624 KB
testcase_01 AC 93 ms
71,680 KB
testcase_02 AC 93 ms
71,732 KB
testcase_03 AC 94 ms
71,724 KB
testcase_04 AC 93 ms
71,496 KB
testcase_05 AC 94 ms
71,540 KB
testcase_06 AC 94 ms
71,524 KB
testcase_07 AC 96 ms
71,616 KB
testcase_08 AC 94 ms
71,504 KB
testcase_09 AC 93 ms
71,500 KB
testcase_10 AC 96 ms
77,860 KB
testcase_11 AC 91 ms
71,600 KB
testcase_12 AC 92 ms
71,684 KB
testcase_13 AC 95 ms
71,492 KB
testcase_14 AC 220 ms
84,176 KB
testcase_15 AC 213 ms
84,088 KB
testcase_16 AC 208 ms
84,004 KB
testcase_17 AC 215 ms
83,992 KB
testcase_18 AC 171 ms
83,408 KB
testcase_19 AC 175 ms
83,952 KB
testcase_20 AC 246 ms
84,732 KB
testcase_21 AC 245 ms
84,496 KB
testcase_22 AC 211 ms
77,928 KB
testcase_23 AC 311 ms
79,284 KB
testcase_24 AC 264 ms
78,996 KB
testcase_25 AC 559 ms
88,020 KB
testcase_26 AC 225 ms
84,264 KB
testcase_27 AC 219 ms
83,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():

    def __init__(self, n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rank = [0]*(n+1)

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

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

        if(x == y):
            return
        elif(self.rank[x] > self.rank[y]):
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if(self.rank[x] == self.rank[y]):
                self.rank[y] += 1

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

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

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

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

    def group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members
        
N,M=map(int,input().split())
uf=UnionFind(2*N+1)
for i in range(M):
    A,B=map(int,input().split())
    uf.unite(A,N+B)
    uf.unite(N+A,B)
for i in range(1,N+1):
    if not uf.same(i,N+i):
        print('No')
        exit(0)
print('Yes')
0