結果

問題 No.1390 Get together
ユーザー 👑 KazunKazun
提出日時 2021-02-12 21:53:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 287 ms / 2,000 ms
コード長 5,151 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 87,368 KB
実行使用メモリ 95,880 KB
最終ジャッジ日時 2023-09-27 03:47:38
合計ジャッジ時間 7,578 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
71,660 KB
testcase_01 AC 69 ms
71,400 KB
testcase_02 AC 64 ms
71,400 KB
testcase_03 AC 102 ms
77,716 KB
testcase_04 AC 98 ms
77,796 KB
testcase_05 AC 102 ms
77,876 KB
testcase_06 AC 107 ms
78,268 KB
testcase_07 AC 98 ms
77,808 KB
testcase_08 AC 100 ms
77,828 KB
testcase_09 AC 106 ms
78,136 KB
testcase_10 AC 65 ms
71,180 KB
testcase_11 AC 66 ms
71,616 KB
testcase_12 AC 67 ms
71,528 KB
testcase_13 AC 65 ms
71,520 KB
testcase_14 AC 66 ms
71,644 KB
testcase_15 AC 66 ms
71,516 KB
testcase_16 AC 246 ms
85,648 KB
testcase_17 AC 201 ms
95,880 KB
testcase_18 AC 240 ms
82,468 KB
testcase_19 AC 269 ms
90,916 KB
testcase_20 AC 276 ms
92,220 KB
testcase_21 AC 271 ms
94,944 KB
testcase_22 AC 269 ms
88,216 KB
testcase_23 AC 257 ms
87,976 KB
testcase_24 AC 261 ms
87,904 KB
testcase_25 AC 275 ms
90,720 KB
testcase_26 AC 287 ms
91,180 KB
testcase_27 AC 281 ms
90,684 KB
testcase_28 AC 276 ms
90,812 KB
testcase_29 AC 280 ms
90,684 KB
testcase_30 AC 273 ms
90,724 KB
testcase_31 AC 272 ms
90,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

        N:要素数
        """
        self.n=N
        self.parents=[-1]*N
        self.rank=[0]*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

        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 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 len(self.roots())

    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 refresh(self):
        for i in range(self.n):
            _=self.find(i)

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

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

#=================================================
class Coloring_Union_Find():
    def __init__(self,N,f,e=0):
        """0,1,...,n-1を要素として初期化する.

        N:要素数
        f:2変数関数の合成
        e:最初の値
        """
        self.n=N
        self.parents=[-1]*N
        self.data=[e]*N
        self.rank=[0]*N
        self.f=f

    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.data[x]=self.data[y]=self.f(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
        return

    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 len(self.roots())

    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__()
#================================================
import sys
N,M=map(int,input().split())
U=Union_Find(M)
X=[-1]*(N+1)
for _ in range(N):
    b,c=map(int,input().split())
    if X[c]==-1:
        X[c]=b-1
    else:
        U.union(b-1,X[c])

print(M-U.group_count())
0