結果
問題 | No.1390 Get together |
ユーザー | tzyvrn |
提出日時 | 2021-02-12 23:45:13 |
言語 | Kotlin (1.9.23) |
結果 |
AC
|
実行時間 | 882 ms / 2,000 ms |
コード長 | 2,966 bytes |
コンパイル時間 | 16,158 ms |
コンパイル使用メモリ | 461,076 KB |
実行使用メモリ | 97,008 KB |
最終ジャッジ日時 | 2024-07-20 01:40:12 |
合計ジャッジ時間 | 38,236 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 313 ms
51,780 KB |
testcase_01 | AC | 317 ms
51,912 KB |
testcase_02 | AC | 314 ms
51,612 KB |
testcase_03 | AC | 470 ms
55,356 KB |
testcase_04 | AC | 461 ms
54,964 KB |
testcase_05 | AC | 472 ms
55,400 KB |
testcase_06 | AC | 457 ms
55,344 KB |
testcase_07 | AC | 461 ms
55,128 KB |
testcase_08 | AC | 467 ms
55,196 KB |
testcase_09 | AC | 470 ms
55,840 KB |
testcase_10 | AC | 321 ms
51,548 KB |
testcase_11 | AC | 316 ms
51,812 KB |
testcase_12 | AC | 320 ms
51,944 KB |
testcase_13 | AC | 317 ms
51,716 KB |
testcase_14 | AC | 320 ms
51,720 KB |
testcase_15 | AC | 317 ms
51,536 KB |
testcase_16 | AC | 858 ms
96,572 KB |
testcase_17 | AC | 817 ms
97,008 KB |
testcase_18 | AC | 847 ms
96,528 KB |
testcase_19 | AC | 833 ms
96,572 KB |
testcase_20 | AC | 882 ms
96,644 KB |
testcase_21 | AC | 826 ms
96,332 KB |
testcase_22 | AC | 852 ms
96,432 KB |
testcase_23 | AC | 852 ms
96,572 KB |
testcase_24 | AC | 858 ms
96,540 KB |
testcase_25 | AC | 837 ms
96,472 KB |
testcase_26 | AC | 857 ms
96,372 KB |
testcase_27 | AC | 872 ms
96,400 KB |
testcase_28 | AC | 850 ms
96,732 KB |
testcase_29 | AC | 836 ms
96,704 KB |
testcase_30 | AC | 845 ms
96,744 KB |
testcase_31 | AC | 869 ms
96,604 KB |
ソースコード
import java.util.* 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 (n, m) = readLine()!!.split(" ").map { it.toInt() } val slimes = mutableListOf<Pair<Int, Int>>() for (i in 0 until n) { val (b, c) = readLine()!!.split(" ").map { it.toInt() } slimes.add(Pair(b - 1, c - 1)) } val dsu = DSU(m) val color_leaders = MutableList(n) { -1 } var ans = 0 for ((box, color) in slimes) { if (color_leaders[color] == -1) { color_leaders[color] = box } else { if (!dsu.same(box, color_leaders[color])) { dsu.merge(box, color_leaders[color]) ans += 1 } } } 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 } }