結果

問題 No.1293 2種類の道路
ユーザー trineutrontrineutron
提出日時 2020-11-21 03:55:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 601 ms / 2,000 ms
コード長 857 bytes
コンパイル時間 413 ms
コンパイル使用メモリ 86,720 KB
実行使用メモリ 97,940 KB
最終ジャッジ日時 2023-09-30 20:34:22
合計ジャッジ時間 8,317 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,200 KB
testcase_01 AC 71 ms
70,944 KB
testcase_02 AC 71 ms
71,132 KB
testcase_03 AC 69 ms
71,240 KB
testcase_04 AC 71 ms
71,188 KB
testcase_05 AC 71 ms
71,148 KB
testcase_06 AC 70 ms
70,860 KB
testcase_07 AC 70 ms
71,108 KB
testcase_08 AC 73 ms
70,944 KB
testcase_09 AC 587 ms
90,944 KB
testcase_10 AC 555 ms
88,868 KB
testcase_11 AC 562 ms
88,788 KB
testcase_12 AC 562 ms
89,184 KB
testcase_13 AC 601 ms
90,148 KB
testcase_14 AC 350 ms
97,940 KB
testcase_15 AC 335 ms
97,136 KB
testcase_16 AC 299 ms
91,176 KB
testcase_17 AC 312 ms
93,500 KB
testcase_18 AC 252 ms
92,888 KB
testcase_19 AC 344 ms
96,244 KB
testcase_20 AC 328 ms
97,052 KB
testcase_21 AC 221 ms
77,704 KB
testcase_22 AC 225 ms
77,904 KB
testcase_23 AC 227 ms
78,164 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