結果

問題 No.2290 UnUnion Find
ユーザー 👑 箱
提出日時 2023-03-15 14:51:26
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 732 ms / 2,000 ms
コード長 2,408 bytes
コンパイル時間 14,918 ms
コンパイル使用メモリ 447,020 KB
実行使用メモリ 70,652 KB
最終ジャッジ日時 2023-10-24 06:50:15
合計ジャッジ時間 52,378 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 274 ms
53,888 KB
testcase_01 AC 278 ms
53,888 KB
testcase_02 AC 576 ms
61,948 KB
testcase_03 AC 636 ms
64,448 KB
testcase_04 AC 701 ms
70,440 KB
testcase_05 AC 705 ms
67,044 KB
testcase_06 AC 686 ms
64,512 KB
testcase_07 AC 695 ms
67,788 KB
testcase_08 AC 654 ms
65,016 KB
testcase_09 AC 717 ms
70,248 KB
testcase_10 AC 695 ms
67,824 KB
testcase_11 AC 702 ms
70,652 KB
testcase_12 AC 677 ms
64,408 KB
testcase_13 AC 643 ms
64,476 KB
testcase_14 AC 655 ms
64,436 KB
testcase_15 AC 693 ms
64,412 KB
testcase_16 AC 701 ms
67,132 KB
testcase_17 AC 651 ms
64,612 KB
testcase_18 AC 643 ms
64,440 KB
testcase_19 AC 705 ms
64,540 KB
testcase_20 AC 674 ms
64,472 KB
testcase_21 AC 645 ms
67,452 KB
testcase_22 AC 732 ms
64,216 KB
testcase_23 AC 684 ms
62,276 KB
testcase_24 AC 661 ms
64,460 KB
testcase_25 AC 719 ms
66,788 KB
testcase_26 AC 663 ms
64,448 KB
testcase_27 AC 676 ms
64,460 KB
testcase_28 AC 673 ms
62,432 KB
testcase_29 AC 675 ms
64,516 KB
testcase_30 AC 684 ms
68,032 KB
testcase_31 AC 679 ms
64,508 KB
testcase_32 AC 731 ms
63,612 KB
testcase_33 AC 650 ms
62,404 KB
testcase_34 AC 656 ms
64,428 KB
testcase_35 AC 669 ms
64,556 KB
testcase_36 AC 696 ms
62,288 KB
testcase_37 AC 687 ms
62,384 KB
testcase_38 AC 705 ms
62,384 KB
testcase_39 AC 703 ms
62,372 KB
testcase_40 AC 673 ms
64,488 KB
testcase_41 AC 718 ms
63,620 KB
testcase_42 AC 670 ms
62,916 KB
testcase_43 AC 669 ms
64,452 KB
testcase_44 AC 709 ms
66,592 KB
testcase_45 AC 674 ms
64,436 KB
testcase_46 AC 657 ms
64,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fun PrintWriter.solve() {
    val n = nextInt()
    val q = nextInt()
    val uf = UnionFind(n)
    var now = 0
    val rnd = Random()
    val candidates = mutableListOf<Int>()
    case@ for (i in 0 until q) {
        val t = nextInt()
        if (t == 1) {
            val u = nextInt() - 1
            val v = nextInt() - 1
            uf.unite(u, v)
        } else {
            val v = nextInt() - 1
            if (uf.size(v) == n) {
                println(-1)
            } else {
                for (w in candidates) {
                    if (!uf.same(v, w)) {
                        println(w + 1)
                        continue@case
                    }
                }
                while (uf.same(v, now)) {
                    now = rnd.nextInt(n)
                }
                println(now + 1)
                candidates.add(now)
            }
        }
    }
}

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