結果
問題 | No.1293 2種類の道路 |
ユーザー | nehan_der_thal |
提出日時 | 2020-11-21 20:56:10 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 477 ms / 2,000 ms |
コード長 | 1,963 bytes |
コンパイル時間 | 224 ms |
コンパイル使用メモリ | 82,488 KB |
実行使用メモリ | 133,728 KB |
最終ジャッジ日時 | 2024-07-23 16:05:25 |
合計ジャッジ時間 | 6,425 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
54,132 KB |
testcase_01 | AC | 44 ms
54,420 KB |
testcase_02 | AC | 42 ms
54,196 KB |
testcase_03 | AC | 42 ms
54,140 KB |
testcase_04 | AC | 42 ms
55,424 KB |
testcase_05 | AC | 45 ms
55,592 KB |
testcase_06 | AC | 41 ms
56,024 KB |
testcase_07 | AC | 42 ms
54,492 KB |
testcase_08 | AC | 42 ms
55,152 KB |
testcase_09 | AC | 422 ms
107,656 KB |
testcase_10 | AC | 477 ms
106,836 KB |
testcase_11 | AC | 422 ms
106,984 KB |
testcase_12 | AC | 436 ms
107,204 KB |
testcase_13 | AC | 439 ms
107,016 KB |
testcase_14 | AC | 286 ms
113,056 KB |
testcase_15 | AC | 280 ms
113,600 KB |
testcase_16 | AC | 300 ms
112,760 KB |
testcase_17 | AC | 298 ms
113,912 KB |
testcase_18 | AC | 255 ms
123,228 KB |
testcase_19 | AC | 307 ms
133,508 KB |
testcase_20 | AC | 308 ms
133,728 KB |
testcase_21 | AC | 156 ms
76,296 KB |
testcase_22 | AC | 159 ms
76,892 KB |
testcase_23 | AC | 160 ms
76,704 KB |
ソースコード
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)