結果

問題 No.1293 2種類の道路
ユーザー NatsubiSoganNatsubiSogan
提出日時 2020-11-21 00:05:41
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,142 ms / 2,000 ms
コード長 1,194 bytes
コンパイル時間 336 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 50,740 KB
最終ジャッジ日時 2024-07-23 14:07:29
合計ジャッジ時間 14,906 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

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