結果

問題 No.1293 2種類の道路
ユーザー H3PO4H3PO4
提出日時 2022-03-05 09:03:00
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 625 ms / 2,000 ms
コード長 1,521 bytes
コンパイル時間 221 ms
コンパイル使用メモリ 10,940 KB
実行使用メモリ 30,664 KB
最終ジャッジ日時 2023-09-26 14:36:56
合計ジャッジ時間 11,027 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,988 KB
testcase_01 AC 16 ms
8,020 KB
testcase_02 AC 16 ms
8,052 KB
testcase_03 AC 16 ms
7,988 KB
testcase_04 AC 16 ms
8,112 KB
testcase_05 AC 17 ms
8,068 KB
testcase_06 AC 16 ms
8,148 KB
testcase_07 AC 16 ms
8,144 KB
testcase_08 AC 18 ms
8,208 KB
testcase_09 AC 609 ms
21,260 KB
testcase_10 AC 597 ms
21,244 KB
testcase_11 AC 625 ms
21,288 KB
testcase_12 AC 607 ms
21,356 KB
testcase_13 AC 602 ms
21,284 KB
testcase_14 AC 417 ms
30,592 KB
testcase_15 AC 416 ms
30,532 KB
testcase_16 AC 504 ms
23,088 KB
testcase_17 AC 508 ms
25,096 KB
testcase_18 AC 360 ms
30,400 KB
testcase_19 AC 436 ms
30,664 KB
testcase_20 AC 432 ms
30,440 KB
testcase_21 AC 327 ms
8,140 KB
testcase_22 AC 320 ms
8,112 KB
testcase_23 AC 317 ms
8,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

input = sys.stdin.buffer.readline


class UnionFind:
    __slots__ = ["n", "parent", "height", "size"]

    def __init__(self, n):
        self.n = n
        self.parent = [i for i in range(n)]
        self.height = [1] * n
        self.size = [1] * n

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.height[x] < self.height[y]:
                self.parent[x] = y
                self.size[y] += self.size[x]
            else:
                self.parent[y] = x
                self.size[x] += self.size[y]
                if self.height[x] == self.height[y]:
                    self.height[x] += 1

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

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


N, D, W = map(int, input().split())
uf_d = UnionFind(N)
for _ in range(D):
    a, b = (int(x) - 1 for x in input().split())
    uf_d.unite(a, b)
uf_w = UnionFind(N)
for _ in range(W):
    c, d = (int(x) - 1 for x in input().split())
    uf_w.unite(c, d)

ans = -N

root_pair_set = set()
for i in range(N):
    root_d = uf_d.find(i)
    root_w = uf_w.find(i)
    if (root_d, root_w) in root_pair_set:
        continue
    root_pair_set.add((root_d, root_w))
    ans += uf_d.size[root_d] * uf_w.size[root_w]
print(ans)
0