結果

問題 No.1390 Get together
ユーザー gorugo30gorugo30
提出日時 2021-02-12 21:39:23
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 913 bytes
コンパイル時間 362 ms
コンパイル使用メモリ 87,300 KB
実行使用メモリ 120,904 KB
最終ジャッジ日時 2023-09-27 03:18:49
合計ジャッジ時間 8,969 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
71,024 KB
testcase_01 AC 61 ms
71,484 KB
testcase_02 AC 61 ms
71,564 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 RE -
testcase_15 AC 60 ms
71,292 KB
testcase_16 WA -
testcase_17 AC 245 ms
120,904 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, N):
        self.par = [-1] * N
        self.size = [1] * N
    def root(self, x):
        while self.par[x] >= 0:
            x = self.par[x]
        return x
    def union(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        if rx == ry:
            return
        if self.size[rx] < self.size[ry]:
            rx, ry = ry, rx
        self.par[ry] = rx
        self.size[rx] += self.size[ry]
    
    def find(self, x, y):
        return self.root(x) == self.root(y)

N, M = map(int, input().split())
box = UnionFind(M + 1)
bi = {}
info = [list(map(int, input().split())) for i in range(M)]
for i in range(M):
    b, c = info[i]
    if bi.get(c, 0) == 0:
        bi[c] = b
    else:
        box.union(b, bi[c])
ans = 0
done = [False] * (M + 1)
for i in range(1, M + 1):
    p = box.root(i)
    if not done[p]:
        ans += box.size[p] - 1
print(ans)
0