結果

問題 No.1390 Get together
ユーザー tktk_snsntktk_snsn
提出日時 2021-02-12 21:32:35
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 376 ms / 2,000 ms
コード長 1,278 bytes
コンパイル時間 1,250 ms
コンパイル使用メモリ 86,924 KB
実行使用メモリ 102,460 KB
最終ジャッジ日時 2023-09-27 03:03:18
合計ジャッジ時間 8,731 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,760 KB
testcase_01 AC 91 ms
71,788 KB
testcase_02 AC 92 ms
71,488 KB
testcase_03 AC 120 ms
77,796 KB
testcase_04 AC 117 ms
77,792 KB
testcase_05 AC 118 ms
77,524 KB
testcase_06 AC 112 ms
77,692 KB
testcase_07 AC 112 ms
77,648 KB
testcase_08 AC 112 ms
77,372 KB
testcase_09 AC 120 ms
77,680 KB
testcase_10 AC 94 ms
71,744 KB
testcase_11 AC 95 ms
71,764 KB
testcase_12 AC 93 ms
71,656 KB
testcase_13 AC 95 ms
71,496 KB
testcase_14 AC 91 ms
71,496 KB
testcase_15 AC 92 ms
71,652 KB
testcase_16 AC 156 ms
86,796 KB
testcase_17 AC 245 ms
102,460 KB
testcase_18 AC 154 ms
86,508 KB
testcase_19 AC 321 ms
100,064 KB
testcase_20 AC 345 ms
100,340 KB
testcase_21 AC 344 ms
100,868 KB
testcase_22 AC 243 ms
96,688 KB
testcase_23 AC 249 ms
97,384 KB
testcase_24 AC 253 ms
96,784 KB
testcase_25 AC 329 ms
100,200 KB
testcase_26 AC 311 ms
100,572 KB
testcase_27 AC 322 ms
100,664 KB
testcase_28 AC 322 ms
99,740 KB
testcase_29 AC 320 ms
100,028 KB
testcase_30 AC 325 ms
99,896 KB
testcase_31 AC 376 ms
101,352 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