結果

問題 No.1390 Get together
ユーザー paruf4paruf4
提出日時 2021-02-12 23:00:30
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,529 bytes
コンパイル時間 263 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 134,068 KB
最終ジャッジ日時 2023-09-27 06:08:28
合計ジャッジ時間 12,711 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,168 KB
testcase_01 AC 71 ms
71,384 KB
testcase_02 AC 71 ms
71,484 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 74 ms
71,632 KB
testcase_15 AC 73 ms
71,428 KB
testcase_16 AC 289 ms
104,516 KB
testcase_17 AC 414 ms
126,080 KB
testcase_18 AC 293 ms
113,936 KB
testcase_19 AC 534 ms
133,676 KB
testcase_20 AC 540 ms
133,016 KB
testcase_21 AC 558 ms
134,068 KB
testcase_22 AC 432 ms
127,904 KB
testcase_23 AC 442 ms
128,016 KB
testcase_24 AC 443 ms
128,036 KB
testcase_25 AC 538 ms
133,968 KB
testcase_26 AC 535 ms
133,596 KB
testcase_27 AC 542 ms
133,560 KB
testcase_28 AC 532 ms
132,752 KB
testcase_29 AC 526 ms
133,676 KB
testcase_30 AC 523 ms
133,048 KB
testcase_31 AC 532 ms
133,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def size(self, x):
        return abs(self.parents[self.find(x)])

    def groups(self):
        roots = self.roots()
        r_to_g = {}
        for i, r in enumerate(roots):
            r_to_g[r] = i
        groups = [[] for _ in roots]
        for i in range(self.n):
            groups[r_to_g[self.find(i)]].append(i)
        return groups


n, m = map(int, input().split())

G = [set() for _ in range(n)]
for i in range(n):
    b, c = map(int, input().split())
    b -= 1
    c -= 1
    G[c].add(b)

uf = UnionFind(n)
G = [list(g) for g in G]
res = 0
for g in G:
    for i in range(len(g)-1):
        if uf.same(g[i],g[i+1]):
            continue
        uf.union(g[i],g[i+1])
        res += 1

print(res)
0