結果

問題 No.1390 Get together
ユーザー とりゐとりゐ
提出日時 2022-05-28 16:32:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 439 ms / 2,000 ms
コード長 962 bytes
コンパイル時間 175 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 112,144 KB
最終ジャッジ日時 2023-10-20 23:13:53
合計ジャッジ時間 10,030 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,684 KB
testcase_01 AC 42 ms
55,684 KB
testcase_02 AC 43 ms
55,684 KB
testcase_03 AC 98 ms
76,972 KB
testcase_04 AC 91 ms
76,804 KB
testcase_05 AC 92 ms
76,924 KB
testcase_06 AC 91 ms
76,816 KB
testcase_07 AC 90 ms
76,792 KB
testcase_08 AC 93 ms
77,020 KB
testcase_09 AC 102 ms
77,024 KB
testcase_10 AC 43 ms
55,684 KB
testcase_11 AC 43 ms
55,684 KB
testcase_12 AC 43 ms
55,684 KB
testcase_13 AC 42 ms
55,684 KB
testcase_14 AC 43 ms
55,684 KB
testcase_15 AC 42 ms
55,684 KB
testcase_16 AC 213 ms
96,704 KB
testcase_17 AC 335 ms
112,144 KB
testcase_18 AC 199 ms
94,752 KB
testcase_19 AC 425 ms
103,752 KB
testcase_20 AC 409 ms
106,332 KB
testcase_21 AC 439 ms
107,716 KB
testcase_22 AC 314 ms
100,556 KB
testcase_23 AC 324 ms
100,184 KB
testcase_24 AC 327 ms
100,168 KB
testcase_25 AC 415 ms
103,620 KB
testcase_26 AC 408 ms
103,664 KB
testcase_27 AC 420 ms
104,120 KB
testcase_28 AC 405 ms
100,388 KB
testcase_29 AC 411 ms
103,376 KB
testcase_30 AC 394 ms
103,648 KB
testcase_31 AC 415 ms
104,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#UnionFind
from collections import defaultdict

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

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

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

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

n,m=map(int,input().split())
uf=UnionFind(m+1)
color=[[] for i in range(n+1)]
for _ in range(n):
  b,c=map(int,input().split())
  color[c].append(b)

for i in range(n+1):
  for j in color[i][1:]:
    uf.union(color[i][0],j)

ans=0
for i in uf.roots():
  ans+=-uf.parents[i]-1

print(ans)
0