結果
問題 | No.1293 2種類の道路 |
ユーザー | semisagi |
提出日時 | 2020-11-20 22:00:08 |
言語 | Swift (5.10.0) |
結果 |
AC
|
実行時間 | 366 ms / 2,000 ms |
コード長 | 1,939 bytes |
コンパイル時間 | 2,580 ms |
コンパイル使用メモリ | 133,712 KB |
実行使用メモリ | 22,528 KB |
最終ジャッジ日時 | 2024-07-23 13:00:16 |
合計ジャッジ時間 | 8,054 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 8 ms
9,344 KB |
testcase_01 | AC | 9 ms
9,472 KB |
testcase_02 | AC | 9 ms
9,472 KB |
testcase_03 | AC | 9 ms
9,344 KB |
testcase_04 | AC | 9 ms
9,344 KB |
testcase_05 | AC | 9 ms
9,344 KB |
testcase_06 | AC | 9 ms
9,344 KB |
testcase_07 | AC | 8 ms
9,472 KB |
testcase_08 | AC | 9 ms
9,472 KB |
testcase_09 | AC | 366 ms
15,232 KB |
testcase_10 | AC | 358 ms
15,104 KB |
testcase_11 | AC | 362 ms
15,232 KB |
testcase_12 | AC | 356 ms
15,104 KB |
testcase_13 | AC | 356 ms
15,232 KB |
testcase_14 | AC | 197 ms
16,084 KB |
testcase_15 | AC | 198 ms
16,012 KB |
testcase_16 | AC | 320 ms
17,280 KB |
testcase_17 | AC | 331 ms
17,340 KB |
testcase_18 | AC | 195 ms
19,420 KB |
testcase_19 | AC | 205 ms
22,528 KB |
testcase_20 | AC | 207 ms
22,528 KB |
testcase_21 | AC | 261 ms
9,344 KB |
testcase_22 | AC | 261 ms
9,344 KB |
testcase_23 | AC | 259 ms
9,472 KB |
ソースコード
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<Int>](repeating: [], count: N) for i in 0 ..< N { set[d.find(i)].insert(w.find(i)) } var answer = 0 for i in 0 ..< N where d.find(i) == i { answer += (set[i].map { w.count($0) }.reduce(0, +)) * d.count(i) } print(answer - N)