結果
問題 | No.1293 2種類の道路 |
ユーザー |
|
提出日時 | 2020-11-20 22:38:19 |
言語 | PyPy3 (7.9.16) |
結果 |
AC
|
実行時間 | 718 ms / 2,000 ms |
コード長 | 1,464 bytes |
コンパイル時間 | 465 ms |
実行使用メモリ | 112,116 KB |
最終ジャッジ日時 | 2023-02-23 19:09:40 |
合計ジャッジ時間 | 10,348 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge11 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 73 ms
75,756 KB |
testcase_01 | AC | 72 ms
75,656 KB |
testcase_02 | AC | 72 ms
75,596 KB |
testcase_03 | AC | 71 ms
75,448 KB |
testcase_04 | AC | 73 ms
75,636 KB |
testcase_05 | AC | 72 ms
75,636 KB |
testcase_06 | AC | 73 ms
75,680 KB |
testcase_07 | AC | 74 ms
75,816 KB |
testcase_08 | AC | 74 ms
75,788 KB |
testcase_09 | AC | 687 ms
96,856 KB |
testcase_10 | AC | 696 ms
95,452 KB |
testcase_11 | AC | 702 ms
99,480 KB |
testcase_12 | AC | 718 ms
96,908 KB |
testcase_13 | AC | 691 ms
95,284 KB |
testcase_14 | AC | 458 ms
110,320 KB |
testcase_15 | AC | 441 ms
112,116 KB |
testcase_16 | AC | 449 ms
99,480 KB |
testcase_17 | AC | 455 ms
104,864 KB |
testcase_18 | AC | 372 ms
106,888 KB |
testcase_19 | AC | 421 ms
104,188 KB |
testcase_20 | AC | 426 ms
104,316 KB |
testcase_21 | AC | 376 ms
83,584 KB |
testcase_22 | AC | 376 ms
83,064 KB |
testcase_23 | AC | 356 ms
83,212 KB |
ソースコード
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def same(self, x, y): return self.find(x) == self.find(y) def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def size(self,x): return abs(self.parents[self.find(x)]) def groups(self): roots = self.roots() r_to_g = {} for i, r in enumerate(roots): r_to_g[r] = i groups = [[] for _ in roots] for i in range(self.n): groups[r_to_g[self.find(i)]].append(i) return groups N, D, W = map(int, input().split()) uf1 = UnionFind(N) uf2 = UnionFind(N) for i in range(D): a,b = map(int, input().split()) uf1.union(a-1,b-1) for i in range(W): c,d = map(int, input().split()) uf2.union(c-1,d-1) ans = 0 for g in uf2.groups(): goal = uf2.size(g[0]) start = 0 check = {} for v in g: r = uf1.find(v) if r not in check: start += uf1.size(r) check[r] = 1 ans += (start-1)*goal print(ans)