結果

問題 No.1390 Get together
ユーザー tktk_snsntktk_snsn
提出日時 2021-02-12 21:32:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 324 ms / 2,000 ms
コード長 1,278 bytes
コンパイル時間 196 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 102,292 KB
最終ジャッジ日時 2024-07-19 20:27:16
合計ジャッジ時間 6,382 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
54,016 KB
testcase_01 AC 43 ms
54,016 KB
testcase_02 AC 44 ms
53,632 KB
testcase_03 AC 71 ms
70,656 KB
testcase_04 AC 70 ms
69,376 KB
testcase_05 AC 70 ms
70,912 KB
testcase_06 AC 66 ms
67,968 KB
testcase_07 AC 63 ms
67,840 KB
testcase_08 AC 59 ms
67,456 KB
testcase_09 AC 67 ms
71,296 KB
testcase_10 AC 45 ms
54,016 KB
testcase_11 AC 43 ms
53,888 KB
testcase_12 AC 42 ms
54,272 KB
testcase_13 AC 44 ms
54,144 KB
testcase_14 AC 43 ms
54,016 KB
testcase_15 AC 42 ms
54,092 KB
testcase_16 AC 116 ms
85,980 KB
testcase_17 AC 195 ms
102,292 KB
testcase_18 AC 116 ms
85,980 KB
testcase_19 AC 285 ms
101,772 KB
testcase_20 AC 297 ms
102,016 KB
testcase_21 AC 297 ms
101,864 KB
testcase_22 AC 197 ms
97,492 KB
testcase_23 AC 200 ms
97,488 KB
testcase_24 AC 214 ms
97,552 KB
testcase_25 AC 273 ms
101,888 KB
testcase_26 AC 256 ms
101,888 KB
testcase_27 AC 270 ms
101,860 KB
testcase_28 AC 270 ms
101,972 KB
testcase_29 AC 266 ms
101,760 KB
testcase_30 AC 278 ms
101,688 KB
testcase_31 AC 324 ms
102,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)
        self.rank = [0] * (n + 1)

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

    def isSame(self, x, y):
        return self.find(x) == self.find(y)

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        return True

    def size(self, x):
        return -self.root[self.find(x)]


N, M = map(int, input().split())
d = defaultdict(list)
for _ in range(N):
    b, c = map(int, input().split())
    b -= 1
    c -= 1
    d[c].append(b)

ans = 0
uf = UF_tree(M)
for k, val in d.items():
    if len(val) == 1:
        continue
    a = val.pop()
    while val:
        b = val.pop()
        ans += uf.unite(a, b)
        a = b
print(ans)
0