結果

問題 No.1293 2種類の道路
ユーザー hir355hir355
提出日時 2020-11-20 22:23:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 547 ms / 2,000 ms
コード長 1,239 bytes
コンパイル時間 401 ms
コンパイル使用メモリ 87,068 KB
実行使用メモリ 98,812 KB
最終ジャッジ日時 2023-09-30 19:31:39
合計ジャッジ時間 7,720 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
70,848 KB
testcase_01 AC 71 ms
70,796 KB
testcase_02 AC 74 ms
70,952 KB
testcase_03 AC 71 ms
70,888 KB
testcase_04 AC 73 ms
71,108 KB
testcase_05 AC 71 ms
70,980 KB
testcase_06 AC 72 ms
70,552 KB
testcase_07 AC 72 ms
71,092 KB
testcase_08 AC 75 ms
70,784 KB
testcase_09 AC 547 ms
89,856 KB
testcase_10 AC 535 ms
89,116 KB
testcase_11 AC 543 ms
90,020 KB
testcase_12 AC 537 ms
89,520 KB
testcase_13 AC 527 ms
90,792 KB
testcase_14 AC 334 ms
97,380 KB
testcase_15 AC 332 ms
98,812 KB
testcase_16 AC 299 ms
91,396 KB
testcase_17 AC 298 ms
90,344 KB
testcase_18 AC 252 ms
93,848 KB
testcase_19 AC 338 ms
98,772 KB
testcase_20 AC 335 ms
97,608 KB
testcase_21 AC 223 ms
78,192 KB
testcase_22 AC 227 ms
77,912 KB
testcase_23 AC 214 ms
77,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n + 1)
        self.size = [1] * (n + 1)

    # 検索
    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):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


n, d, w = map(int, input().split())
ufd = UnionFind(n)
ufw = UnionFind(n)
for i in range(d):
    a, b = map(int, input().split())
    ufd.unite(a - 1, b - 1)
for i in range(w):
    c, d = map(int, input().split())
    ufw.unite(c - 1, d - 1)
s = set()
for i in range(n):
    s.add((ufd.find(i), ufw.find(i)))
ans = 0
for p, q in s:
    ans += ufd.size[p] * ufw.size[q]
print(ans - n)
0