結果

問題 No.1390 Get together
ユーザー O2MTO2MT
提出日時 2021-02-13 13:45:34
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,549 bytes
コンパイル時間 287 ms
コンパイル使用メモリ 86,844 KB
実行使用メモリ 146,364 KB
最終ジャッジ日時 2023-09-27 22:08:35
合計ジャッジ時間 11,057 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,712 KB
testcase_01 AC 76 ms
71,616 KB
testcase_02 AC 75 ms
71,252 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 AC 78 ms
71,588 KB
testcase_15 AC 76 ms
71,472 KB
testcase_16 AC 237 ms
113,460 KB
testcase_17 AC 333 ms
146,364 KB
testcase_18 AC 223 ms
106,532 KB
testcase_19 AC 403 ms
117,828 KB
testcase_20 AC 421 ms
136,996 KB
testcase_21 AC 398 ms
126,872 KB
testcase_22 AC 317 ms
115,828 KB
testcase_23 AC 321 ms
118,152 KB
testcase_24 AC 331 ms
118,628 KB
testcase_25 AC 393 ms
117,084 KB
testcase_26 AC 386 ms
115,732 KB
testcase_27 AC 394 ms
118,524 KB
testcase_28 AC 396 ms
117,956 KB
testcase_29 AC 385 ms
117,304 KB
testcase_30 AC 383 ms
118,268 KB
testcase_31 AC 382 ms
116,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 size(self, x):
        return -self.parents[self.find(x)]
 
    def same(self, x, y):
        return self.find(x) == self.find(y)
 
    def members(self, 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):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members
 

N,M = map(int,input().split())
l = [[] for _ in range(N)]
uf = UnionFind(N)

for i in range(N):
    b,c = map(int,input().split())
    b -= 1
    c -= 1
    l[c].append(b)

for i in range(N):
    for j in range(len(l[i])-1):
        uf.union(l[i][j],l[i][j+1])

A = list(uf.all_group_members().values())
group_cnt = len(A)
ans = N-group_cnt
print(ans)
0