結果

問題 No.2202 贅沢てりたまチキン
ユーザー n_nan_na
提出日時 2023-02-05 02:21:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 573 ms / 2,000 ms
コード長 1,757 bytes
コンパイル時間 599 ms
コンパイル使用メモリ 87,000 KB
実行使用メモリ 92,104 KB
最終ジャッジ日時 2023-09-16 21:33:48
合計ジャッジ時間 6,830 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,404 KB
testcase_01 AC 74 ms
71,196 KB
testcase_02 AC 75 ms
71,028 KB
testcase_03 AC 74 ms
71,064 KB
testcase_04 AC 75 ms
71,256 KB
testcase_05 AC 74 ms
71,380 KB
testcase_06 AC 76 ms
71,444 KB
testcase_07 AC 73 ms
71,260 KB
testcase_08 AC 73 ms
71,188 KB
testcase_09 AC 74 ms
71,532 KB
testcase_10 AC 76 ms
80,812 KB
testcase_11 AC 72 ms
71,284 KB
testcase_12 AC 71 ms
71,440 KB
testcase_13 AC 71 ms
71,204 KB
testcase_14 AC 200 ms
87,816 KB
testcase_15 AC 200 ms
88,128 KB
testcase_16 AC 190 ms
86,828 KB
testcase_17 AC 195 ms
86,724 KB
testcase_18 AC 158 ms
92,104 KB
testcase_19 AC 154 ms
86,772 KB
testcase_20 AC 231 ms
88,124 KB
testcase_21 AC 227 ms
88,068 KB
testcase_22 AC 192 ms
77,612 KB
testcase_23 AC 307 ms
80,900 KB
testcase_24 AC 246 ms
79,736 KB
testcase_25 AC 573 ms
91,776 KB
testcase_26 AC 206 ms
88,160 KB
testcase_27 AC 200 ms
87,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    
    # インスタンス変数の初期化
    # インスタンス変数へアクセスするときは "self.インスタンス変数"
    def __init__(self, n):
        self.par = [-1]*n # 要素の根(親)
        self.rank = [0]*n # 要素が属している木の高さ
        self.siz = [1]*n # 要素が属している木の大きさ(要素数)

    # xのroot(Find)
    def root(self, x):
        if self.par[x] == -1:
            return x
        else:
            # 経路圧縮(xの親: par[x]を根に設定する)
            self.par[x] = self.root(self.par[x])
            return self.par[x]
    
    # xとyをmerge(Union)
    def merge(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        if rx == ry: return False
        
        # union by rank(rankが大きい方:rxに小さい方:ryをmerge)
        if self.rank[rx] < self.rank[ry]:
            rx,ry = ry,rx # rx: 親, ry: 子
        self.par[ry] = rx
        if self.rank[rx] == self.rank[ry]:
            self.rank[rx] += 1
            
        # 集合サイズを更新
        self.siz[rx] += self.siz[ry]
        
        return True
            
    # xとyが同一のgroupかどうか
    def issame(self, x, y):
        return self.root(x) == self.root(y)
    
    # xが含まれる木のサイズ
    def size(self, x):
        return self.siz[self.root(x)]

#---------------------------------------------------
N,M = map(int,input().split())
uf = UnionFind(2*N+10)

for _ in range(M):
    A,B = map(int,input().split())
    uf.merge(A,B+N)
    uf.merge(A+N,B)
    
res = True
for i in range(1,N+1):
    if not uf.issame(i,i+N):
        res = False
        break
        
if res: print("Yes")
else: print("No")
0