結果

問題 No.1293 2種類の道路
ユーザー NatsubiSoganNatsubiSogan
提出日時 2020-11-21 00:05:41
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,106 ms / 2,000 ms
コード長 1,194 bytes
コンパイル時間 221 ms
コンパイル使用メモリ 11,112 KB
実行使用メモリ 48,424 KB
最終ジャッジ日時 2023-09-30 20:17:47
合計ジャッジ時間 14,653 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,152 KB
testcase_01 AC 16 ms
8,384 KB
testcase_02 AC 16 ms
8,224 KB
testcase_03 AC 16 ms
8,004 KB
testcase_04 AC 17 ms
8,160 KB
testcase_05 AC 16 ms
8,180 KB
testcase_06 AC 16 ms
8,052 KB
testcase_07 AC 16 ms
8,148 KB
testcase_08 AC 17 ms
8,228 KB
testcase_09 AC 1,106 ms
42,112 KB
testcase_10 AC 1,070 ms
42,272 KB
testcase_11 AC 1,064 ms
42,308 KB
testcase_12 AC 1,081 ms
42,284 KB
testcase_13 AC 1,106 ms
42,212 KB
testcase_14 AC 730 ms
48,360 KB
testcase_15 AC 726 ms
48,424 KB
testcase_16 AC 971 ms
44,000 KB
testcase_17 AC 956 ms
44,048 KB
testcase_18 AC 652 ms
43,996 KB
testcase_19 AC 729 ms
42,100 KB
testcase_20 AC 713 ms
42,000 KB
testcase_21 AC 627 ms
8,568 KB
testcase_22 AC 617 ms
8,644 KB
testcase_23 AC 613 ms
8,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.par = list(range(self.n))
        self.rank = [1] * n
        self.count = n
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    def unite(self, x, y):
        p = self.find(x)
        q = self.find(y)
        if p == q:
            return None
        if p > q:
            p, q = q, p
        self.rank[p] += self.rank[q]
        self.par[q] = p
        self.count -= 1
    def same(self, x, y):
        return self.find(x) == self.find(y)
    def size(self, x):
        return self.rank[x]
    def count(self):
        return self.count
n, d, w = map(int, input().split())
UF1, UF2 = UnionFind(n), UnionFind(n)
for i in range(d):
    a, b = map(int, input().split())
    UF1.unite(a - 1, b - 1)
for i in range(w):
    c, d = map(int, input().split())
    UF2.unite(c - 1, d - 1)
l = [0] * n
s = [set() for i in range(n)]
for i in range(n):
    x, y = UF1.find(i), UF2.find(i)
    if y not in s[x]:
        s[x].add(y)
        l[x] += UF2.size(y)
print(sum(l[UF1.find(i)] - 1 for i in range(n)))
0