結果

問題 No.1293 2種類の道路
ユーザー tamatotamato
提出日時 2020-11-20 21:37:04
言語 PyPy3
(7.3.8)
結果
AC  
実行時間 379 ms / 2,000 ms
コード長 1,976 bytes
コンパイル時間 458 ms
使用メモリ 106,508 KB
最終ジャッジ日時 2023-02-23 18:02:17
合計ジャッジ時間 6,933 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 84 ms
77,016 KB
testcase_01 AC 85 ms
76,904 KB
testcase_02 AC 86 ms
76,824 KB
testcase_03 AC 86 ms
77,048 KB
testcase_04 AC 86 ms
76,824 KB
testcase_05 AC 86 ms
77,036 KB
testcase_06 AC 85 ms
76,980 KB
testcase_07 AC 85 ms
76,944 KB
testcase_08 AC 84 ms
76,984 KB
testcase_09 AC 367 ms
97,944 KB
testcase_10 AC 355 ms
97,828 KB
testcase_11 AC 368 ms
97,504 KB
testcase_12 AC 364 ms
97,568 KB
testcase_13 AC 379 ms
98,192 KB
testcase_14 AC 254 ms
106,508 KB
testcase_15 AC 249 ms
106,380 KB
testcase_16 AC 281 ms
97,740 KB
testcase_17 AC 268 ms
99,640 KB
testcase_18 AC 216 ms
94,612 KB
testcase_19 AC 210 ms
89,340 KB
testcase_20 AC 210 ms
89,648 KB
testcase_21 AC 168 ms
85,228 KB
testcase_22 AC 166 ms
85,232 KB
testcase_23 AC 170 ms
85,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    from collections import deque
    input = sys.stdin.buffer.readline

    class UnionFind():
        def __init__(self, n):
            self.n = n
            self.root = [-1] * (n + 1)
            self.rnk = [0] * (n + 1)

        def find_root(self, x):
            while self.root[x] >= 0:
                x = self.root[x]
            return x

        def unite(self, x, y):
            x = self.find_root(x)
            y = self.find_root(y)
            if x == y:
                return
            elif self.rnk[x] > self.rnk[y]:
                self.root[x] += self.root[y]
                self.root[y] = x
            else:
                self.root[y] += self.root[x]
                self.root[x] = y
                if self.rnk[x] == self.rnk[y]:
                    self.rnk[y] += 1

        def isSameGroup(self, x, y):
            return self.find_root(x) == self.find_root(y)

        def size(self, x):
            return -self.root[self.find_root(x)]

    N, D, W = map(int, input().split())
    adj = [[] for _ in range(N+1)]
    for _ in range(D):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)
    UF = UnionFind(N)
    for _ in range(W):
        a, b = map(int, input().split())
        UF.unite(a, b)

    seen = [0] * (N+1)
    ans = -N
    for v0 in range(1, N+1):
        if seen[v0]:
            continue
        seen[v0] = 1
        seen_w = set()
        que = deque()
        que.append(v0)
        cnt_d = 0
        cnt_w = 0
        while que:
            v = que.popleft()
            cnt_d += 1
            r = UF.find_root(v)
            if r not in seen_w:
                seen_w.add(r)
                cnt_w += -UF.root[r]
            for u in adj[v]:
                if not seen[u]:
                    seen[u] = 1
                    que.append(u)
        ans += cnt_w * cnt_d
    print(ans)


if __name__ == '__main__':
    main()
0