結果

問題 No.1390 Get together
ユーザー rlangevinrlangevin
提出日時 2023-01-18 19:48:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 407 ms / 2,000 ms
コード長 1,208 bytes
コンパイル時間 1,449 ms
コンパイル使用メモリ 86,716 KB
実行使用メモリ 130,728 KB
最終ジャッジ日時 2023-09-02 13:34:24
合計ジャッジ時間 11,781 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,260 KB
testcase_01 AC 71 ms
71,320 KB
testcase_02 AC 70 ms
71,192 KB
testcase_03 AC 129 ms
78,316 KB
testcase_04 AC 104 ms
78,100 KB
testcase_05 AC 107 ms
78,444 KB
testcase_06 AC 105 ms
78,164 KB
testcase_07 AC 104 ms
77,932 KB
testcase_08 AC 103 ms
78,124 KB
testcase_09 AC 113 ms
78,256 KB
testcase_10 AC 72 ms
71,008 KB
testcase_11 AC 70 ms
71,316 KB
testcase_12 AC 69 ms
71,412 KB
testcase_13 AC 70 ms
71,224 KB
testcase_14 AC 70 ms
71,212 KB
testcase_15 AC 69 ms
71,316 KB
testcase_16 AC 189 ms
106,196 KB
testcase_17 AC 274 ms
130,728 KB
testcase_18 AC 187 ms
94,644 KB
testcase_19 AC 407 ms
119,308 KB
testcase_20 AC 392 ms
126,716 KB
testcase_21 AC 389 ms
125,996 KB
testcase_22 AC 273 ms
110,120 KB
testcase_23 AC 280 ms
111,484 KB
testcase_24 AC 283 ms
111,400 KB
testcase_25 AC 387 ms
118,976 KB
testcase_26 AC 379 ms
118,208 KB
testcase_27 AC 381 ms
118,648 KB
testcase_28 AC 398 ms
118,940 KB
testcase_29 AC 386 ms
118,684 KB
testcase_30 AC 376 ms
119,196 KB
testcase_31 AC 378 ms
118,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline

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):
        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 = 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):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]

N, M = map(int, readline().split())
U = UnionFind(M)
L = [[] for i in range(N)]
for i in range(N):
    b, c = map(int, readline().split())
    b, c = b - 1, c - 1
    L[c].append(b)
    
for i in range(N):
    for j in range(len(L[i]) - 1):
        U.union(L[i][j], L[i][j + 1])
S = set()
for i in range(M):
    S.add(U.find(i))
print(M - len(S))
0