結果

問題 No.1293 2種類の道路
ユーザー AEnAEn
提出日時 2023-01-19 01:41:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 885 ms / 2,000 ms
コード長 3,038 bytes
コンパイル時間 812 ms
コンパイル使用メモリ 87,260 KB
実行使用メモリ 116,708 KB
最終ジャッジ日時 2023-09-02 18:34:50
合計ジャッジ時間 15,237 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 176 ms
80,996 KB
testcase_01 AC 175 ms
81,088 KB
testcase_02 AC 177 ms
80,884 KB
testcase_03 AC 179 ms
80,852 KB
testcase_04 AC 177 ms
80,932 KB
testcase_05 AC 178 ms
81,004 KB
testcase_06 AC 174 ms
81,004 KB
testcase_07 AC 174 ms
81,012 KB
testcase_08 AC 178 ms
81,068 KB
testcase_09 AC 885 ms
111,888 KB
testcase_10 AC 845 ms
111,904 KB
testcase_11 AC 857 ms
110,504 KB
testcase_12 AC 806 ms
113,648 KB
testcase_13 AC 803 ms
109,560 KB
testcase_14 AC 456 ms
116,388 KB
testcase_15 AC 461 ms
116,708 KB
testcase_16 AC 466 ms
106,948 KB
testcase_17 AC 485 ms
111,456 KB
testcase_18 AC 413 ms
105,624 KB
testcase_19 AC 627 ms
101,888 KB
testcase_20 AC 609 ms
102,616 KB
testcase_21 AC 400 ms
90,040 KB
testcase_22 AC 412 ms
90,380 KB
testcase_23 AC 350 ms
87,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
from typing import List
class UnionFind:
    """0-indexed"""
    def __init__(self, n):
        self.n = n
        self.parent = [-1] * n
        self.__group_count = n

    def unite(self, x, y) -> bool:
        """xとyをマージ"""
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return False

        self.__group_count -= 1

        if self.parent[x] > self.parent[y]:
            x, y = y, x

        self.parent[x] += self.parent[y]
        self.parent[y] = x
        return True

    def is_same(self, x, y) -> bool:
        """xとyが同じ連結成分か判定"""
        return self.root(x) == self.root(y)

    def root(self, x) -> int:
        """xの根を取得"""
        if self.parent[x] < 0:
            return x
        else:
            # 経路圧縮あり
            # self.parent[x] = self.root(self.parent[x])
            # return self.parent[x]
            # 経路圧縮なし
            return self.root(self.parent[x])

    def size(self, x) -> int:
        """xが属する連結成分のサイズを取得"""
        return -self.parent[self.root(x)]

    def all_sizes(self) -> List[int]:
        """全連結成分のサイズのリストを取得 O(N)"""
        sizes = []
        for i in range(self.n):
            size = self.parent[i]
            if size < 0:
                sizes.append(-size)
        return sizes
    
    def members(self, x) -> List[int]:
        """xが属するグループのリストを返す O(N)"""
        mem = []
        r = self.root(x)
        for i in range(self.n):
            if self.root(i) == r:
                mem.append(i)
        return mem

    def groups(self) -> List[List[int]]:
        """全連結成分の内容のリストを取得 O(N・α(N))"""
        groups = dict()
        for i in range(self.n):
            p = self.root(i)
            if not groups.get(p):
                groups[p] = []
            groups[p].append(i)
        return list(groups.values())

    @property
    def group_count(self) -> int:
        """連結成分の数を取得 O(1)"""
        return self.__group_count

N, D, W = map(int, input().split())
G = [list() for _ in range(N)]
uf = UnionFind(N)
for i in range(D):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
for i in range(W):
    c, d = map(int, input().split())
    uf.unite(c-1,d-1)

seen = set()
d = deque()
res = 0
for i in range(N):
    if i not in seen:
        s = 1
        t = 0
        used = set()
        used.add(uf.root(i))
        t += uf.size(i)
        seen.add(i)
        d.append(i)
        while d:
            v = d.popleft()
            for nex in G[v]:
                if nex not in seen:
                    seen.add(nex)
                    s += 1
                    r = uf.root(nex)
                    if r not in used:
                        used.add(r)
                        t += uf.size(nex)
                    d.append(nex)
        res += s*t-s
print(res)          
0