struct Scanner { private var elements = [String]() private var index = 0 mutating func peek() -> String { while elements.count == index { elements = readLine()!.split(separator: " ").map(String.init) index = 0 } return elements[index] } mutating func next() -> String { defer { index += 1 } return peek() } mutating func nextInt() -> Int { return Int(next())! } mutating func nextInts(_ n: Int) -> [Int] { return (0 ..< n).map { _ in nextInt() } } mutating func nextDouble() -> Double { return Double(next())! } } struct UnionFind { var parent: [Int] var count: [Int] init(_ n: Int) { parent = Array(0 ..< n) count = [Int](repeating: 1, count: n) } mutating func find(_ v: Int) -> Int { if v == parent[v] { return v } parent[v] = find(parent[v]) return parent[v] } mutating func union(_ u: Int, _ v: Int) { var u = find(u) var v = find(v) guard u != v else { return } if count[u] < count[v] { swap(&u, &v) } count[u] += count[v] parent[v] = u } mutating func count(_ u: Int) -> Int { count[find(u)] } } var scanner = Scanner() let N = scanner.nextInt() let D = scanner.nextInt() let W = scanner.nextInt() var d = UnionFind(N) var w = UnionFind(N) for _ in 0 ..< D { let a = scanner.nextInt() - 1 let b = scanner.nextInt() - 1 d.union(a, b) } for _ in 0 ..< W { let c = scanner.nextInt() - 1 let d = scanner.nextInt() - 1 w.union(c, d) } var set = [Set](repeating: [], count: N) for i in 0 ..< N { set[d.find(i)].insert(w.find(i)) } var answer = 0 for i in 0 ..< N { answer += set[d.find(i)].map { w.count($0) }.reduce(0, +) } print(answer - N)