結果

問題 No.1390 Get together
ユーザー ygd.ygd.
提出日時 2021-02-12 21:36:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 755 ms / 2,000 ms
コード長 1,843 bytes
コンパイル時間 476 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 138,976 KB
最終ジャッジ日時 2023-09-27 03:13:13
合計ジャッジ時間 13,892 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,464 KB
testcase_01 AC 93 ms
71,432 KB
testcase_02 AC 92 ms
71,228 KB
testcase_03 AC 171 ms
79,276 KB
testcase_04 AC 157 ms
78,960 KB
testcase_05 AC 161 ms
79,192 KB
testcase_06 AC 154 ms
79,324 KB
testcase_07 AC 149 ms
78,916 KB
testcase_08 AC 154 ms
79,304 KB
testcase_09 AC 165 ms
79,424 KB
testcase_10 AC 95 ms
71,524 KB
testcase_11 AC 93 ms
71,568 KB
testcase_12 AC 94 ms
71,744 KB
testcase_13 AC 93 ms
71,776 KB
testcase_14 AC 96 ms
71,620 KB
testcase_15 AC 93 ms
71,504 KB
testcase_16 AC 408 ms
118,884 KB
testcase_17 AC 383 ms
131,220 KB
testcase_18 AC 369 ms
123,032 KB
testcase_19 AC 703 ms
137,952 KB
testcase_20 AC 641 ms
133,248 KB
testcase_21 AC 656 ms
134,596 KB
testcase_22 AC 536 ms
129,872 KB
testcase_23 AC 572 ms
130,596 KB
testcase_24 AC 594 ms
130,648 KB
testcase_25 AC 755 ms
138,976 KB
testcase_26 AC 664 ms
138,184 KB
testcase_27 AC 673 ms
137,576 KB
testcase_28 AC 682 ms
138,380 KB
testcase_29 AC 658 ms
138,924 KB
testcase_30 AC 680 ms
138,024 KB
testcase_31 AC 676 ms
138,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]
    def find(self, x):
        """
        x が属するグループを探索して親を出す。
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]
    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)
    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]

from collections import defaultdict
color = defaultdict(list) 
N,M = map(int,input().split())
B = []; C = []
box = [[] for _ in range(M)]
for i in range(N):
    b,c = map(int,input().split())
    b-=1 #箱は0-index
    B.append(b);C.append(c)
    color[c].append(i)
    box[b].append(i)
uf = UnionFind(N)
#同じ箱にいるものをくっ付ける。
for nakami in box:
    if not nakami: continue #中身が空なら次
    par = nakami[0]
    for i in range(1,len(nakami)):
        uf.union(par,nakami[i])
        
ans = 0
for iro in color.values():
    if not iro: continue
    par = iro[0]
    for i in range(1,len(iro)):
        if not uf.is_same(par,iro[i]):
            uf.union(par,iro[i])
            ans += 1
print(ans)
0