結果
| 問題 |
No.1390 Get together
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-02-12 22:08:19 |
| 言語 | Kotlin (2.1.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 1,361 bytes |
| コンパイル時間 | 15,021 ms |
| コンパイル使用メモリ | 447,348 KB |
| 実行使用メモリ | 102,396 KB |
| 最終ジャッジ日時 | 2024-07-19 22:01:23 |
| 合計ジャッジ時間 | 37,034 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 6 WA * 12 RE * 11 |
コンパイルメッセージ
Main.kt:6:13: warning: variable 'm' is never used
val (n, m) = readLine()!!.split(" ").map { it.toInt() }
^
ソースコード
import kotlin.collections.*
import kotlin.math.*
@kotlin.ExperimentalStdlibApi
fun main() {
val (n, m) = readLine()!!.split(" ").map { it.toInt() }
val uni = UnionFind(n)
val c2b = mutableMapOf<Int, Int>()
var ans = 0
for (i in 0 until n) {
val (b, c) = readLine()!!.split(" ").map { it.toInt() }
val box = c2b.get(c)
when (box) {
null -> c2b[c] = b
else -> {
if (uni.isSame(box, b)) continue
ans++
c2b[c] = uni.root(b)
}
}
}
println(ans)
}
data class UnionFind(private val n : Int) {
private data class node(var parent : Int? = null, var size : Int = 1)
private val nodes = Array(n+1) { node() }
fun root(x : Int): Int {
return when(nodes[x].parent) {
null -> x
else -> { nodes[x].parent = root(nodes[x].parent!!); nodes[x].parent!! }
}
}
fun merge(x : Int, y : Int) : Boolean{
var rx = root(x); var ry = root(y)
if (rx == ry) return false
if (nodes[rx].size < nodes[ry].size) rx = ry.also { ry = rx }
nodes[ry].parent = rx; nodes[rx].size += nodes[ry].size
return true
}
fun isSame(x : Int, y : Int) : Boolean { return root(x) == root(y) }
fun size(x : Int) : Int { return nodes[root(x)].size }
}