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] UF2.snapshot() for v in group[1:]: UF2.union(u, v) ans += UF1.size(u) * UF2.size(u) UF2.rollback() print(ans)