結果

問題 No.1293 2種類の道路
ユーザー 👑 rin204rin204
提出日時 2022-11-11 16:06:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,001 ms / 2,000 ms
コード長 2,445 bytes
コンパイル時間 602 ms
コンパイル使用メモリ 87,180 KB
実行使用メモリ 134,000 KB
最終ジャッジ日時 2023-10-11 12:52:16
合計ジャッジ時間 12,559 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,580 KB
testcase_01 AC 73 ms
71,548 KB
testcase_02 AC 73 ms
71,392 KB
testcase_03 AC 73 ms
71,576 KB
testcase_04 AC 75 ms
71,500 KB
testcase_05 AC 73 ms
71,456 KB
testcase_06 AC 74 ms
71,164 KB
testcase_07 AC 73 ms
71,424 KB
testcase_08 AC 75 ms
71,468 KB
testcase_09 AC 988 ms
132,412 KB
testcase_10 AC 989 ms
133,944 KB
testcase_11 AC 1,001 ms
131,888 KB
testcase_12 AC 940 ms
134,000 KB
testcase_13 AC 941 ms
132,220 KB
testcase_14 AC 597 ms
112,948 KB
testcase_15 AC 561 ms
114,076 KB
testcase_16 AC 406 ms
117,040 KB
testcase_17 AC 411 ms
123,448 KB
testcase_18 AC 327 ms
106,780 KB
testcase_19 AC 626 ms
122,740 KB
testcase_20 AC 597 ms
125,060 KB
testcase_21 AC 444 ms
103,108 KB
testcase_22 AC 456 ms
102,348 KB
testcase_23 AC 308 ms
99,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class RollbackUnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = n
        self.history = []
        self.inner_snap = 0

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            return self.find(self.parents[x])

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        self.history.append((x, self.parents[x]))
        self.history.append((y, self.parents[y]))

        if x == y:
            return False
        self.group -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x

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

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

    def undo(self):
        x, p = self.history.pop()
        self.parents[x] = p
        y, p = self.history.pop()
        self.parents[y] = p
        if x != y:
            self.group += 1

    def snapshot(self):
        self.inner_snap = len(self.history) >> 1

    def get_state(self):
        return len(self.history) >> 1

    def rollback(self, state=-1):
        if state == -1:
            state = self.inner_snap
        state <<= 1
        assert state <= len(self.history)
        
        while state < len(self.history):
            self.undo()


n, d, w = map(int, input().split())
UF1 = RollbackUnionFind(n)
UF2 = RollbackUnionFind(n)
for _ in range(d):
    a, b = map(int, input().split())
    UF1.union(a - 1, b - 1)
for _ in range(w):
    a, b = map(int, input().split())
    UF2.union(a - 1, b - 1)

ans = -n
for group in UF1.all_group_members().values():
    u = group[0]
    ver = UF2.get_state()
    for v in group[1:]:
        UF2.union(u, v)

    ans += UF1.size(u) * UF2.size(u)
    UF2.rollback(ver)

print(ans)



0