結果

問題 No.2290 UnUnion Find
ユーザー 👑 箱
提出日時 2023-03-06 00:04:24
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 701 ms / 2,000 ms
コード長 2,190 bytes
コンパイル時間 17,173 ms
コンパイル使用メモリ 446,408 KB
実行使用メモリ 69,868 KB
最終ジャッジ日時 2023-10-24 06:47:53
合計ジャッジ時間 52,892 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 270 ms
53,980 KB
testcase_01 AC 278 ms
53,972 KB
testcase_02 AC 585 ms
62,088 KB
testcase_03 AC 694 ms
69,732 KB
testcase_04 AC 684 ms
64,628 KB
testcase_05 AC 677 ms
65,180 KB
testcase_06 AC 645 ms
65,928 KB
testcase_07 AC 650 ms
64,504 KB
testcase_08 AC 680 ms
69,868 KB
testcase_09 AC 660 ms
64,576 KB
testcase_10 AC 654 ms
64,612 KB
testcase_11 AC 664 ms
67,000 KB
testcase_12 AC 651 ms
65,184 KB
testcase_13 AC 701 ms
66,792 KB
testcase_14 AC 651 ms
64,520 KB
testcase_15 AC 691 ms
69,728 KB
testcase_16 AC 660 ms
64,596 KB
testcase_17 AC 693 ms
65,324 KB
testcase_18 AC 691 ms
65,036 KB
testcase_19 AC 644 ms
64,432 KB
testcase_20 AC 645 ms
60,228 KB
testcase_21 AC 633 ms
58,488 KB
testcase_22 AC 632 ms
58,812 KB
testcase_23 AC 640 ms
58,820 KB
testcase_24 AC 630 ms
59,684 KB
testcase_25 AC 634 ms
58,840 KB
testcase_26 AC 650 ms
60,032 KB
testcase_27 AC 644 ms
59,792 KB
testcase_28 AC 648 ms
59,288 KB
testcase_29 AC 630 ms
59,716 KB
testcase_30 AC 631 ms
58,620 KB
testcase_31 AC 636 ms
59,632 KB
testcase_32 AC 696 ms
59,164 KB
testcase_33 AC 628 ms
62,392 KB
testcase_34 AC 638 ms
64,548 KB
testcase_35 AC 634 ms
64,516 KB
testcase_36 AC 636 ms
62,220 KB
testcase_37 AC 635 ms
62,372 KB
testcase_38 AC 640 ms
62,180 KB
testcase_39 AC 648 ms
62,360 KB
testcase_40 AC 642 ms
64,468 KB
testcase_41 AC 669 ms
62,452 KB
testcase_42 AC 643 ms
64,440 KB
testcase_43 AC 637 ms
64,444 KB
testcase_44 AC 655 ms
64,548 KB
testcase_45 AC 624 ms
64,512 KB
testcase_46 AC 615 ms
64,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fun PrintWriter.solve() {
    val n = nextInt()
    val q = nextInt()
    var min = 0
    var max = n - 1
    val uf = UnionFind(n)
    for (i in 0 until q) {
        val t = nextInt()
        if (t == 1) {
            val u = nextInt() - 1
            val v = nextInt() - 1
            uf.unite(u, v)
            while (max != uf.find(max)) max--
            while (min != uf.find(min)) min++
        } else {
            val v = nextInt() - 1
            if (!uf.same(v, min)) {
                println(min + 1)
            } else if (!uf.same(v, max)) {
                println(max + 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