結果

問題 No.1293 2種類の道路
ユーザー Kiri8128Kiri8128
提出日時 2020-11-20 22:26:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 346 ms / 2,000 ms
コード長 1,470 bytes
コンパイル時間 281 ms
コンパイル使用メモリ 87,180 KB
実行使用メモリ 97,276 KB
最終ジャッジ日時 2023-09-30 19:33:22
合計ジャッジ時間 6,549 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
70,984 KB
testcase_01 AC 75 ms
71,028 KB
testcase_02 AC 74 ms
70,988 KB
testcase_03 AC 75 ms
71,192 KB
testcase_04 AC 76 ms
71,224 KB
testcase_05 AC 74 ms
71,216 KB
testcase_06 AC 74 ms
71,132 KB
testcase_07 AC 74 ms
71,128 KB
testcase_08 AC 76 ms
70,996 KB
testcase_09 AC 346 ms
87,560 KB
testcase_10 AC 339 ms
87,896 KB
testcase_11 AC 340 ms
87,676 KB
testcase_12 AC 342 ms
88,212 KB
testcase_13 AC 338 ms
88,160 KB
testcase_14 AC 237 ms
97,164 KB
testcase_15 AC 231 ms
97,276 KB
testcase_16 AC 276 ms
90,148 KB
testcase_17 AC 276 ms
91,788 KB
testcase_18 AC 234 ms
93,676 KB
testcase_19 AC 239 ms
96,412 KB
testcase_20 AC 241 ms
96,052 KB
testcase_21 AC 222 ms
77,768 KB
testcase_22 AC 220 ms
77,792 KB
testcase_23 AC 219 ms
77,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda: sys.stdin.readline().rstrip()
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.PA = [-1] * n
    def root(self, a):
        L = []
        while self.PA[a] >= 0:
            L.append(a)
            a = self.PA[a]
        for l in L:
            self.PA[l] = a
        return a
    def unite(self, a, b):
        ra, rb = self.root(a), self.root(b)
        if ra != rb:
            if self.PA[rb] >= self.PA[ra]:
                self.PA[ra] += self.PA[rb]
                self.PA[rb] = ra
            else:
                self.PA[rb] += self.PA[ra]
                self.PA[ra] = rb
    def size(self, a):
        return -self.PA[self.root(a)]
    def groups(self):
        G = [[] for _ in range(self.n)]
        for i in range(self.n):
            G[self.root(i)].append(i)
        return [g for g in G if g]
    def group_size(self):
        G = [[] for _ in range(self.n)]
        for i in range(self.n):
            G[self.root(i)].append(i)
        return [len(g) for g in G if g]

N, D, W = map(int, input().split())
uf1 = UnionFind(N)
uf2 = UnionFind(N)
for _ in range(D):
    a, b = map(int, input().split())
    uf1.unite(a-1, b-1)

for _ in range(W):
    a, b = map(int, input().split())
    uf2.unite(a-1, b-1)

ans = -N
for g in uf1.groups():
    s = len(g)
    S = set()
    for a in g:
        r = uf2.root(a)
        if r not in S:
            S.add(r)
            ans += uf2.size(r) * s

print(ans)
0