結果

問題 No.1293 2種類の道路
ユーザー trineutrontrineutron
提出日時 2020-11-21 03:55:15
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 917 ms / 2,000 ms
コード長 857 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 10,908 KB
実行使用メモリ 28,704 KB
最終ジャッジ日時 2023-09-30 20:34:12
合計ジャッジ時間 12,717 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,232 KB
testcase_01 AC 17 ms
8,100 KB
testcase_02 AC 16 ms
8,268 KB
testcase_03 AC 16 ms
8,200 KB
testcase_04 AC 16 ms
8,220 KB
testcase_05 AC 20 ms
8,220 KB
testcase_06 AC 16 ms
8,016 KB
testcase_07 AC 16 ms
8,092 KB
testcase_08 AC 17 ms
8,236 KB
testcase_09 AC 912 ms
21,508 KB
testcase_10 AC 917 ms
21,508 KB
testcase_11 AC 902 ms
21,336 KB
testcase_12 AC 910 ms
21,320 KB
testcase_13 AC 915 ms
21,316 KB
testcase_14 AC 600 ms
28,552 KB
testcase_15 AC 597 ms
28,544 KB
testcase_16 AC 831 ms
23,320 KB
testcase_17 AC 836 ms
24,116 KB
testcase_18 AC 528 ms
28,704 KB
testcase_19 AC 598 ms
28,396 KB
testcase_20 AC 589 ms
28,508 KB
testcase_21 AC 616 ms
8,324 KB
testcase_22 AC 624 ms
8,328 KB
testcase_23 AC 618 ms
8,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))
        self.size = [1] * n

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

    def merge(self, m, n):
        m = self.find(m)
        n = self.find(n)
        if m == n:
            return
        if m > n:
            m, n = n, m
        self.parent[m] = n
        self.size[n] += self.size[m]


n, d, w = map(int, input().split())
x = UnionFind(n)
y = UnionFind(n)
for i in range(d):
    a, b = map(int, input().split())
    x.merge(a - 1, b - 1)
for i in range(w):
    a, b = map(int, input().split())
    y.merge(a - 1, b - 1)

s = set()
for i in range(n):
    s.add((x.find(i), y.find(i)))
ans = -n
for a, b in s:
    ans += x.size[a] * y.size[b]
print(ans)
0