結果

問題 No.2072 Anatomy
ユーザー 👑 KazunKazun
提出日時 2022-09-16 22:29:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 382 ms / 2,000 ms
コード長 3,545 bytes
コンパイル時間 386 ms
コンパイル使用メモリ 87,204 KB
実行使用メモリ 94,720 KB
最終ジャッジ日時 2023-08-23 16:05:57
合計ジャッジ時間 8,550 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,304 KB
testcase_01 AC 72 ms
71,320 KB
testcase_02 AC 73 ms
71,144 KB
testcase_03 AC 76 ms
75,804 KB
testcase_04 AC 72 ms
71,324 KB
testcase_05 AC 75 ms
75,964 KB
testcase_06 AC 71 ms
71,332 KB
testcase_07 AC 73 ms
75,868 KB
testcase_08 AC 358 ms
90,700 KB
testcase_09 AC 270 ms
89,936 KB
testcase_10 AC 357 ms
91,808 KB
testcase_11 AC 321 ms
92,636 KB
testcase_12 AC 282 ms
88,128 KB
testcase_13 AC 382 ms
94,720 KB
testcase_14 AC 310 ms
87,748 KB
testcase_15 AC 233 ms
87,152 KB
testcase_16 AC 373 ms
93,552 KB
testcase_17 AC 311 ms
92,192 KB
testcase_18 AC 224 ms
85,800 KB
testcase_19 AC 372 ms
94,076 KB
testcase_20 AC 378 ms
94,704 KB
testcase_21 AC 208 ms
92,376 KB
testcase_22 AC 351 ms
94,360 KB
testcase_23 AC 262 ms
92,112 KB
testcase_24 AC 183 ms
92,056 KB
testcase_25 AC 356 ms
93,996 KB
testcase_26 AC 68 ms
71,156 KB
testcase_27 AC 257 ms
92,236 KB
testcase_28 AC 269 ms
93,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Coloring_Union_Find():
    __slots__=["n","parents","rank","data","merge","__group_number"]
    def __init__(self,N,merge,unit):
        """ 0,1,...,N-1 を要素として初期化する.

        N: 要素数
        merge: 合成の方法
        e: 最初の値
        """
        self.n=N
        self.parents=[-1]*N
        self.data=[unit]*N
        self.rank=[0]*N
        self.merge=merge
        self.__group_number=N

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

        x: 要素
        """
        V=[]
        while self.parents[x]>=0:
            V.append(x)
            x=self.parents[x]

        for v in V:
            self.parents[v]=x
        return x

    def union(self, x, y):
        """ 要素 x,y を同一視し, それぞれが持っている族の色を統合する.

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

        if x==y:
            return

        self.__group_number-=1

        self.data[x]=self.data[y]=self.merge(self.data[x],self.data[y])

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

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

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

    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 set(self,x,c):
        """ 要素 x の属する族の色を c に変更する.

        x: 要素
        c: 色
        """
        self.data[self.find(x)]=c

    def look(self,x):
        """ 要素 x の属する成分の色

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

    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 roots(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.roots()}
        for k in range(self.n):
            X[self.find(k)].append(k)
        return X

    def color_list(self):
        return [self.look(x) for x in range(self.n)]

    def color_map(self):
        return {x:self.look(x) for x in self.roots()}

    def __str__(self):
        return '\n'.join('{} [color: {}]: {}'.format(r,self.look(r),self.members(r)) for r in self.roots())

    def __repr__(self):
        return self.__str__()

    def __getitem__(self,index):
        return self.data[self.find(index)]

    def __setitem__(self,index,value):
        self.data[self.find(index)]=value
#==================================================
def solve():
    import sys
    input=sys.stdin.readline
    write=sys.stdout.write

    N,M=map(int,input().split())
    Edge=[]
    for _ in range(M):
        u,v=map(int,input().split())
        Edge.append((u,v))

    Edge.reverse()

    U=Coloring_Union_Find(N+1, max, 0)
    for u,v in Edge:
        U.union(u,v)
        U.set(u, U.look(u)+1)

    return U.look(1)

print(solve())
0