結果

問題 No.2202 贅沢てりたまチキン
ユーザー 👑 KazunKazun
提出日時 2023-02-03 21:31:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 446 ms / 2,000 ms
コード長 3,818 bytes
コンパイル時間 517 ms
コンパイル使用メモリ 87,144 KB
実行使用メモリ 90,172 KB
最終ジャッジ日時 2023-09-15 16:58:21
合計ジャッジ時間 6,151 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,360 KB
testcase_01 AC 75 ms
71,640 KB
testcase_02 AC 74 ms
71,212 KB
testcase_03 AC 73 ms
71,324 KB
testcase_04 AC 73 ms
71,244 KB
testcase_05 AC 73 ms
71,108 KB
testcase_06 AC 73 ms
71,028 KB
testcase_07 AC 74 ms
71,332 KB
testcase_08 AC 75 ms
71,104 KB
testcase_09 AC 72 ms
71,340 KB
testcase_10 AC 87 ms
87,100 KB
testcase_11 AC 73 ms
71,216 KB
testcase_12 AC 73 ms
71,360 KB
testcase_13 AC 72 ms
71,248 KB
testcase_14 AC 223 ms
88,320 KB
testcase_15 AC 232 ms
88,508 KB
testcase_16 AC 180 ms
88,480 KB
testcase_17 AC 188 ms
88,440 KB
testcase_18 AC 139 ms
82,960 KB
testcase_19 AC 149 ms
88,192 KB
testcase_20 AC 226 ms
88,940 KB
testcase_21 AC 227 ms
89,240 KB
testcase_22 AC 212 ms
77,676 KB
testcase_23 AC 289 ms
78,808 KB
testcase_24 AC 252 ms
78,036 KB
testcase_25 AC 446 ms
90,172 KB
testcase_26 AC 237 ms
88,652 KB
testcase_27 AC 229 ms
88,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Union_Find():
    __slots__=("n","parents","rank","edges","__group_number")
    def __init__(self,N):
        """ 0,1,...,N-1 を要素として初期化する.

        N: 要素数
        """
        self.n=N
        self.parents=[-1]*N
        self.rank=[0]*N
        self.edges=[0]*N
        self.__group_number=N

    def find(self, x):
        """ 要素 x の属している族を調べる.

        x: 要素
        """

        a=x
        while self.parents[a]>=0:
            a=self.parents[a]

        while self.parents[x]>=0:
            y=self.parents[x]
            self.parents[x]=a
            x=y

        return a

    def union(self, x, y):
        """ 要素 x,y を同一視する.

        [input]
        x,y: 要素

        [output]
        元々が非連結 → True
        元々が連結 → False
        """
        x=self.find(x)
        y=self.find(y)

        if x==y:
            self.edges[x]+=1
            return False

        if self.rank[x]<self.rank[y]:
            x,y=y,x

        self.__group_number-=1

        self.edges[x]+=self.edges[y]+1
        self.edges[y]=0

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

        if self.rank[x]==self.rank[y]:
            self.rank[x]+=1
        return True

    def size(self, x):
        """ 要素 x の属している族の要素の数.

        x: 要素
        """
        return -self.parents[self.find(x)]

    def same(self, x, y):
        """ 要素 x,y は同一視されているか?

        x,y: 要素
        """
        return self.find(x) == self.find(y)

    def members(self, x):
        """ 要素 x が属している族の要素.
        ※族の要素の個数が欲しいときは size を使うこと!!

        x: 要素
        """
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def edge_count(self, x):
        """ 要素 x が属する族の辺の本数を求める.

        x: 要素
        """
        return self.edges[self.find(x)]

    def is_tree(self, x):
        """ 要素 x が属する族が木かどうかを判定する.

        x: 要素
        """
        return self.size(x)==self.edges[self.find(x)]+1

    def tree_count(self):
        """ 木になっている属の個数を求める.
        """

        return sum(self.is_tree(g) for g in self.representative())

    def representative(self):
        """ 代表元のリスト
        """
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        """ 族の個数
        """

        return self.__group_number

    def all_group_members(self):
        """ 全ての族の出力
        """
        X={r:[] for r in self.representative()}
        for k in range(self.n):
            X[self.find(k)].append(k)
        return X

    def group_list(self):
        """ 各要素が属している族のリストを出力する.

        """
        return [self.find(x) for x in range(self.n)]

    def refresh(self):
        for i in range(self.n):
            _=self.find(i)

    def __str__(self):
        return str(self.all_group_members().values())[13:-2]

    def __repr__(self):
        return "Union Find : "+str(self)

    def __getitem__(self,index):
        return self.find(index)

    def __setitem__(self,x,y):
        self.union(x,y)

#==================================================
def solve():
    N,M=map(int,input().split())

    U=Union_Find(2*N)
    for j in range(M):
        a,b=map(int,input().split())
        a-=1; b-=1

        U.union(a, b+N)
        U.union(a+N, b)
    return all([U.same(i,i+N) for i in range(N)])

#==================================================
import sys
input=sys.stdin.readline
write=sys.stdout.write

print("Yes" if solve() else "No")
0