結果

問題 No.2290 UnUnion Find
ユーザー 👑 箱
提出日時 2023-03-05 23:54:53
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,426 ms / 2,000 ms
コード長 2,322 bytes
コンパイル時間 15,399 ms
コンパイル使用メモリ 452,952 KB
実行使用メモリ 82,552 KB
最終ジャッジ日時 2023-10-24 06:46:59
合計ジャッジ時間 75,166 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 306 ms
56,000 KB
testcase_01 AC 304 ms
56,012 KB
testcase_02 AC 664 ms
62,328 KB
testcase_03 AC 1,186 ms
81,896 KB
testcase_04 AC 1,202 ms
81,892 KB
testcase_05 AC 1,153 ms
81,904 KB
testcase_06 AC 1,202 ms
81,912 KB
testcase_07 AC 1,156 ms
81,900 KB
testcase_08 AC 1,187 ms
81,932 KB
testcase_09 AC 1,164 ms
81,912 KB
testcase_10 AC 1,145 ms
81,892 KB
testcase_11 AC 1,157 ms
81,860 KB
testcase_12 AC 1,183 ms
81,840 KB
testcase_13 AC 1,201 ms
81,904 KB
testcase_14 AC 1,174 ms
81,836 KB
testcase_15 AC 1,143 ms
81,900 KB
testcase_16 AC 1,131 ms
81,896 KB
testcase_17 AC 1,147 ms
80,912 KB
testcase_18 AC 1,178 ms
81,908 KB
testcase_19 AC 1,111 ms
75,652 KB
testcase_20 AC 1,426 ms
81,436 KB
testcase_21 AC 740 ms
62,460 KB
testcase_22 AC 910 ms
66,000 KB
testcase_23 AC 920 ms
69,804 KB
testcase_24 AC 1,269 ms
77,524 KB
testcase_25 AC 917 ms
64,984 KB
testcase_26 AC 1,203 ms
79,520 KB
testcase_27 AC 1,277 ms
79,184 KB
testcase_28 AC 1,113 ms
71,816 KB
testcase_29 AC 1,197 ms
77,180 KB
testcase_30 AC 779 ms
64,516 KB
testcase_31 AC 1,118 ms
75,668 KB
testcase_32 AC 868 ms
64,724 KB
testcase_33 AC 1,006 ms
68,000 KB
testcase_34 AC 1,384 ms
81,444 KB
testcase_35 AC 1,331 ms
79,504 KB
testcase_36 AC 872 ms
64,784 KB
testcase_37 AC 1,012 ms
70,708 KB
testcase_38 AC 876 ms
64,760 KB
testcase_39 AC 951 ms
67,348 KB
testcase_40 AC 1,285 ms
79,548 KB
testcase_41 AC 876 ms
64,956 KB
testcase_42 AC 1,298 ms
73,792 KB
testcase_43 AC 1,113 ms
73,620 KB
testcase_44 AC 1,154 ms
82,080 KB
testcase_45 AC 1,226 ms
82,544 KB
testcase_46 AC 1,275 ms
82,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import java.util.*
import kotlin.math.*

fun PrintWriter.solve() {
    val n = nextInt()
    val q = nextInt()
    val parents = TreeSet<Int>()
    val uf = UnionFind(n)
    (0 until n).forEach { parents.add(it) }
    for (i in 0 until q) {
        val t = nextInt()
        if (t == 1) {
            val u = nextInt() - 1
            val v = nextInt() - 1
            parents.remove(uf.find(u))
            parents.remove(uf.find(v))
            uf.unite(u, v)
            parents.add(uf.find(u))
        } else {
            val v = nextInt() - 1
            val v1 = parents.first()
            val v2 = parents.last()
            if (!uf.same(v, v1)) {
                println(v1 + 1)
            } else if (!uf.same(v, v2)) {
                println(v2 + 1)
            } else {
                println(-1)
            }
        }
    }
}

class UnionFind(n: Int) {
    private val rank = IntArray(n) { 0 }
    private val parent = IntArray(n) { -1 }

    fun find(x: Int): Int {
        if (parent[x] < 0) {
            return x
        }
        parent[x] = find(parent[x])
        return parent[x]
    }

    fun unite(x: Int, y: Int) {
        val x1 = find(x)
        val y1 = find(y)
        if (x1 == y1) {
            return
        }
        if (rank[x1] < rank[y1]) {
            parent[y1] += parent[x1]
            parent[x1] = y1
        } else {
            parent[x1] += parent[y1]
            parent[y1] = x1
            if (rank[x1] == rank[y1]) {
                rank[x1]++
            }
        }
    }

    fun same(x: Int, y: Int) = find(x) == find(y)

    fun size(x: Int) = -parent[find(x)]
}

fun main() {
    Thread(null, {
        val writer = PrintWriter(System.out, false)
        writer.solve()
        writer.flush()
    }, "solve", abs(1L.shl(26)))
        .apply { setUncaughtExceptionHandler { _, e -> e.printStackTrace(); kotlin.system.exitProcess(1) } }
        .apply { start() }.join()
}

// region Scanner
private var st = StringTokenizer("")
private val br = System.`in`.bufferedReader()

fun next(): String {
    while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())
    return st.nextToken()
}

fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextLine() = br.readLine()
fun nextDouble() = next().toDouble()
// endregion
0