結果
問題 | No.2290 UnUnion Find |
ユーザー | 箱星 |
提出日時 | 2023-03-15 14:51:26 |
言語 | Kotlin (1.9.23) |
結果 |
AC
|
実行時間 | 787 ms / 2,000 ms |
コード長 | 2,408 bytes |
コンパイル時間 | 15,231 ms |
コンパイル使用メモリ | 445,188 KB |
実行使用メモリ | 98,320 KB |
最終ジャッジ日時 | 2024-09-22 23:37:26 |
合計ジャッジ時間 | 53,382 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 297 ms
58,472 KB |
testcase_01 | AC | 300 ms
58,488 KB |
testcase_02 | AC | 626 ms
90,252 KB |
testcase_03 | AC | 688 ms
92,860 KB |
testcase_04 | AC | 715 ms
93,416 KB |
testcase_05 | AC | 706 ms
94,316 KB |
testcase_06 | AC | 711 ms
92,940 KB |
testcase_07 | AC | 704 ms
92,876 KB |
testcase_08 | AC | 714 ms
93,256 KB |
testcase_09 | AC | 714 ms
92,704 KB |
testcase_10 | AC | 717 ms
93,128 KB |
testcase_11 | AC | 761 ms
94,388 KB |
testcase_12 | AC | 755 ms
96,144 KB |
testcase_13 | AC | 716 ms
93,072 KB |
testcase_14 | AC | 731 ms
97,960 KB |
testcase_15 | AC | 746 ms
98,320 KB |
testcase_16 | AC | 716 ms
92,864 KB |
testcase_17 | AC | 700 ms
92,924 KB |
testcase_18 | AC | 733 ms
92,808 KB |
testcase_19 | AC | 748 ms
92,884 KB |
testcase_20 | AC | 717 ms
92,728 KB |
testcase_21 | AC | 706 ms
95,952 KB |
testcase_22 | AC | 787 ms
91,540 KB |
testcase_23 | AC | 748 ms
90,732 KB |
testcase_24 | AC | 714 ms
92,812 KB |
testcase_25 | AC | 769 ms
92,792 KB |
testcase_26 | AC | 710 ms
92,688 KB |
testcase_27 | AC | 728 ms
92,744 KB |
testcase_28 | AC | 715 ms
90,828 KB |
testcase_29 | AC | 724 ms
92,704 KB |
testcase_30 | AC | 710 ms
94,228 KB |
testcase_31 | AC | 736 ms
92,772 KB |
testcase_32 | AC | 772 ms
94,540 KB |
testcase_33 | AC | 695 ms
90,564 KB |
testcase_34 | AC | 721 ms
92,864 KB |
testcase_35 | AC | 732 ms
92,744 KB |
testcase_36 | AC | 744 ms
90,624 KB |
testcase_37 | AC | 740 ms
90,816 KB |
testcase_38 | AC | 733 ms
90,912 KB |
testcase_39 | AC | 729 ms
90,568 KB |
testcase_40 | AC | 724 ms
92,920 KB |
testcase_41 | AC | 781 ms
94,548 KB |
testcase_42 | AC | 742 ms
92,928 KB |
testcase_43 | AC | 746 ms
92,816 KB |
testcase_44 | AC | 744 ms
95,056 KB |
testcase_45 | AC | 727 ms
92,868 KB |
testcase_46 | AC | 704 ms
92,688 KB |
ソースコード
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