結果

問題 No.1390 Get together
ユーザー 👑 zeronosu77108zeronosu77108
提出日時 2021-02-12 22:10:34
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 1,361 bytes
コンパイル時間 15,023 ms
コンパイル使用メモリ 432,476 KB
実行使用メモリ 77,208 KB
最終ジャッジ日時 2023-09-27 04:24:45
合計ジャッジ時間 35,505 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 293 ms
53,048 KB
testcase_01 AC 289 ms
52,988 KB
testcase_02 AC 293 ms
53,260 KB
testcase_03 AC 453 ms
57,996 KB
testcase_04 AC 415 ms
56,700 KB
testcase_05 WA -
testcase_06 AC 417 ms
57,104 KB
testcase_07 WA -
testcase_08 AC 414 ms
56,688 KB
testcase_09 AC 455 ms
57,888 KB
testcase_10 AC 289 ms
53,024 KB
testcase_11 WA -
testcase_12 AC 290 ms
53,088 KB
testcase_13 WA -
testcase_14 AC 289 ms
53,036 KB
testcase_15 AC 289 ms
53,208 KB
testcase_16 WA -
testcase_17 AC 872 ms
76,072 KB
testcase_18 AC 781 ms
65,312 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 902 ms
75,768 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 924 ms
75,944 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import kotlin.collections.*
import kotlin.math.*

@kotlin.ExperimentalStdlibApi
fun main() {
    val (n, m) = readLine()!!.split(" ").map { it.toInt() }
    val uni = UnionFind(m)

    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 }
}
0