結果

問題 No.1390 Get together
ユーザー MitarushiMitarushi
提出日時 2021-02-12 23:40:17
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,035 bytes
コンパイル時間 385 ms
コンパイル使用メモリ 86,920 KB
実行使用メモリ 85,132 KB
最終ジャッジ日時 2023-09-27 07:23:34
合計ジャッジ時間 9,244 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,120 KB
testcase_01 AC 71 ms
71,368 KB
testcase_02 AC 75 ms
71,076 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 73 ms
71,516 KB
testcase_11 AC 75 ms
71,316 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 73 ms
71,272 KB
testcase_15 AC 73 ms
71,500 KB
testcase_16 AC 300 ms
83,908 KB
testcase_17 AC 251 ms
83,972 KB
testcase_18 AC 326 ms
85,132 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.parent = [-1] * n
        self.rank = [1] * n
        self.count = [1] * n

    def get_root(self, i):
        while self.parent[i] != -1:
            i = self.parent[i]
        return i

    def is_same_tree(self, i, j):
        return self.get_root(i) == self.get_root(j)

    def merge(self, i, j):
        i = self.get_root(i)
        j = self.get_root(j)
        if i == j:
            return

        if self.rank[i] > self.rank[j]:
            self.parent[j] = i
            self.rank[i] = max(self.rank[i], self.rank[j] + 1)
            self.count[i] += self.count[j]
        else:
            self.parent[i] = j
            self.rank[j] = max(self.rank[j], self.rank[i] + 1)
            self.count[j] += self.count[i]


n, m = map(int, input().split())
col = [-1] * n
uf = UnionFind(m)
for i in range(n):
    b, c = [int(i)-1 for i in input().split()]
    if col[c] != -1:
        uf.merge(b, col[c])
    col[c] = b
ans = 0
for i in uf.count:
    ans += i-1
print(ans)
0