結果
問題 | No.1390 Get together |
ユーザー | tzyvrn |
提出日時 | 2021-02-13 00:17:35 |
言語 | Kotlin (1.9.23) |
結果 |
AC
|
実行時間 | 1,652 ms / 2,000 ms |
コード長 | 2,850 bytes |
コンパイル時間 | 16,336 ms |
コンパイル使用メモリ | 453,932 KB |
実行使用メモリ | 116,172 KB |
最終ジャッジ日時 | 2024-07-20 02:14:11 |
合計ジャッジ時間 | 49,021 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 356 ms
51,040 KB |
testcase_01 | AC | 352 ms
51,248 KB |
testcase_02 | AC | 365 ms
51,116 KB |
testcase_03 | AC | 515 ms
58,860 KB |
testcase_04 | AC | 514 ms
57,464 KB |
testcase_05 | AC | 532 ms
58,568 KB |
testcase_06 | AC | 522 ms
57,800 KB |
testcase_07 | AC | 503 ms
57,628 KB |
testcase_08 | AC | 525 ms
58,040 KB |
testcase_09 | AC | 536 ms
60,028 KB |
testcase_10 | AC | 358 ms
51,168 KB |
testcase_11 | AC | 356 ms
51,276 KB |
testcase_12 | AC | 360 ms
51,124 KB |
testcase_13 | AC | 359 ms
51,240 KB |
testcase_14 | AC | 357 ms
51,456 KB |
testcase_15 | AC | 364 ms
51,140 KB |
testcase_16 | AC | 1,551 ms
107,228 KB |
testcase_17 | AC | 1,554 ms
116,172 KB |
testcase_18 | AC | 1,468 ms
107,300 KB |
testcase_19 | AC | 1,521 ms
115,332 KB |
testcase_20 | AC | 1,547 ms
107,504 KB |
testcase_21 | AC | 1,623 ms
113,128 KB |
testcase_22 | AC | 1,484 ms
108,280 KB |
testcase_23 | AC | 1,522 ms
112,640 KB |
testcase_24 | AC | 1,593 ms
113,892 KB |
testcase_25 | AC | 1,578 ms
111,876 KB |
testcase_26 | AC | 1,517 ms
113,952 KB |
testcase_27 | AC | 1,652 ms
112,232 KB |
testcase_28 | AC | 1,596 ms
113,624 KB |
testcase_29 | AC | 1,634 ms
112,408 KB |
testcase_30 | AC | 1,604 ms
114,892 KB |
testcase_31 | AC | 1,572 ms
113,412 KB |
ソースコード
import java.util.* import kotlin.collections.HashSet fun main() { val sc = Scanner(System.`in`) val n = sc.nextInt() val m = sc.nextInt() val slimes = List(n) { Pair(sc.nextInt() - 1, sc.nextInt() - 1) } val dsu = DSU(m) val color_leaders = MutableList(n) { -1 } for ((box, color) in slimes) { if (color_leaders[color] == -1) { color_leaders[color] = box } else { dsu.merge(box, color_leaders[color]) } } val seen = HashSet<Int>() var ans = 0 for (i in 0 until m) { if (seen.contains(dsu.leader(i))) { continue } ans += dsu.size(i) - 1 seen.add(dsu.leader(i)) } println(ans) } fun eprintln(obj: Any) = System.err.println(obj.toString()) /** * Disjoint set union. * * convert from [AtCoderLibraryForJava - DSU](https://github.com/NASU41/AtCoderLibraryForJava/blob/8b3a3b599790a9ca8c7dd92e036af4ddc5be1b05/DSU/DSU.java) */ class DSU(private val n: Int) { private val parentOrSize: IntArray = IntArray(n) { -1 } /** * Merge nodes. */ fun merge(a: Int, b: Int): Int { if (a !in 0 until n) throw IndexOutOfBoundsException("a=$a") if (b !in 0 until n) throw IndexOutOfBoundsException("b=$b") var x = leader(a) var y = leader(b) if (x == y) return x if (-parentOrSize[x] < -parentOrSize[y]) { val tmp = x x = y y = tmp } parentOrSize[x] += parentOrSize[y] parentOrSize[y] = x return x } /** * True if two nodes are connected. */ fun same(a: Int, b: Int): Boolean { if (a !in 0 until n) throw IndexOutOfBoundsException("a=$a") if (b !in 0 until n) throw IndexOutOfBoundsException("b=$b") return leader(a) == leader(b) } /** * Get its leader node. */ fun leader(a: Int): Int { return if (parentOrSize[a] < 0) { a } else { parentOrSize[a] = leader(parentOrSize[a]) parentOrSize[a] } } /** * A group's size. */ fun size(a: Int): Int { if (a !in 0 until n) throw IndexOutOfBoundsException("" + a) return -parentOrSize[leader(a)] } /** * Group by leader. */ fun groups(): ArrayList<ArrayList<Int>> { val leaderBuf = IntArray(n) val groupSize = IntArray(n) for (i in 0 until n) { leaderBuf[i] = leader(i) groupSize[leaderBuf[i]]++ } val result = ArrayList<ArrayList<Int>>(n) for (i in 0 until n) { result.add(ArrayList(groupSize[i])) } for (i in 0 until n) { result[leaderBuf[i]].add(i) } result.removeIf { it.isEmpty() } return result } }