結果

問題 No.1293 2種類の道路
ユーザー nehan_der_thalnehan_der_thal
提出日時 2020-11-21 20:56:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 534 ms / 2,000 ms
コード長 1,963 bytes
コンパイル時間 550 ms
コンパイル使用メモリ 87,112 KB
実行使用メモリ 138,592 KB
最終ジャッジ日時 2023-09-30 22:18:19
合計ジャッジ時間 8,639 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,592 KB
testcase_01 AC 95 ms
71,688 KB
testcase_02 AC 97 ms
71,816 KB
testcase_03 AC 98 ms
71,712 KB
testcase_04 AC 94 ms
71,736 KB
testcase_05 AC 95 ms
71,600 KB
testcase_06 AC 92 ms
71,632 KB
testcase_07 AC 94 ms
71,596 KB
testcase_08 AC 97 ms
71,652 KB
testcase_09 AC 524 ms
108,592 KB
testcase_10 AC 523 ms
108,948 KB
testcase_11 AC 524 ms
108,324 KB
testcase_12 AC 527 ms
109,144 KB
testcase_13 AC 534 ms
109,236 KB
testcase_14 AC 358 ms
113,820 KB
testcase_15 AC 368 ms
117,568 KB
testcase_16 AC 388 ms
115,544 KB
testcase_17 AC 381 ms
115,796 KB
testcase_18 AC 331 ms
128,880 KB
testcase_19 AC 395 ms
136,740 KB
testcase_20 AC 398 ms
138,592 KB
testcase_21 AC 220 ms
79,388 KB
testcase_22 AC 225 ms
79,284 KB
testcase_23 AC 217 ms
78,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFindNode:
    def __init__(self, group_id, parent=None, value=None):
        self.group_id_ = group_id
        self.parent_ = parent
        self.value = value
        self.rank_ = 1
        self.member_num_ = 1

    def is_root(self):
        return not self.parent_

    def root(self):
        parent = self
        while not parent.is_root():
            parent = parent.parent_
            self.parent_ = parent
        return parent

    def find(self):
        root = self.root()
        return root.group_id_

    def rank(self):
        root = self.root()
        return root.rank_

    def unite(self, unite_node):
        root = self.root()
        unite_root = unite_node.root()

        if root.group_id_ != unite_root.group_id_:
            if root.rank() > unite_root.rank():
                n_root, child = root, unite_root
            else:
                n_root, child = unite_root, root
            child.parent_ = n_root
            n_root.rank_ = max(n_root.rank_, child.rank_ + 1)
            n_root.member_num_ = n_root.member_num_ + child.member_num_

if __name__ == "__main__":
    import sys;input=sys.stdin.readline
    N, D, W = map(int, input().split())
    nodes1 = [UnionFindNode(i) for i in range(N+1)]
    for _ in range(D):
        a, b = map(int, input().split())
        nodes1[a].unite(nodes1[b])
    nodes2 = [UnionFindNode(i) for i in range(N+1)]
    for _ in range(W):
        a, b = map(int, input().split())
        nodes2[a].unite(nodes2[b])

    R = 0
    vs = set()
    from collections import defaultdict
    X = defaultdict(int)
    D = defaultdict(set)
    for i in range(1, N+1):
        x, y = nodes1[i].root().group_id_, nodes2[i].root().group_id_
#        if x in vs:
#            continue
#        vs.add(x)
        if y in D[x]:
            continue
        D[x].add(y)
        X[x] += nodes2[y].member_num_
#    print(D)
    for x in D:
        R += nodes1[x].member_num_ * (X[x]-1)
    print(R)
0