結果
問題 | No.2290 UnUnion Find |
ユーザー | 箱星 |
提出日時 | 2023-03-05 23:54:53 |
言語 | Kotlin (1.9.23) |
結果 |
AC
|
実行時間 | 1,395 ms / 2,000 ms |
コード長 | 2,322 bytes |
コンパイル時間 | 14,336 ms |
コンパイル使用メモリ | 451,028 KB |
実行使用メモリ | 109,044 KB |
最終ジャッジ日時 | 2024-09-22 23:34:24 |
合計ジャッジ時間 | 71,477 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 305 ms
58,484 KB |
testcase_01 | AC | 307 ms
58,512 KB |
testcase_02 | AC | 671 ms
90,640 KB |
testcase_03 | AC | 1,180 ms
108,076 KB |
testcase_04 | AC | 1,197 ms
107,912 KB |
testcase_05 | AC | 1,185 ms
108,100 KB |
testcase_06 | AC | 1,170 ms
108,068 KB |
testcase_07 | AC | 1,173 ms
107,940 KB |
testcase_08 | AC | 1,177 ms
107,988 KB |
testcase_09 | AC | 1,196 ms
108,076 KB |
testcase_10 | AC | 1,195 ms
107,912 KB |
testcase_11 | AC | 1,184 ms
107,880 KB |
testcase_12 | AC | 1,231 ms
108,084 KB |
testcase_13 | AC | 1,213 ms
108,216 KB |
testcase_14 | AC | 1,214 ms
108,176 KB |
testcase_15 | AC | 1,197 ms
108,104 KB |
testcase_16 | AC | 1,186 ms
108,004 KB |
testcase_17 | AC | 1,199 ms
107,220 KB |
testcase_18 | AC | 1,214 ms
108,164 KB |
testcase_19 | AC | 1,210 ms
102,232 KB |
testcase_20 | AC | 1,372 ms
108,680 KB |
testcase_21 | AC | 761 ms
90,736 KB |
testcase_22 | AC | 940 ms
94,620 KB |
testcase_23 | AC | 949 ms
93,140 KB |
testcase_24 | AC | 1,267 ms
104,216 KB |
testcase_25 | AC | 875 ms
91,092 KB |
testcase_26 | AC | 1,293 ms
106,520 KB |
testcase_27 | AC | 1,328 ms
106,000 KB |
testcase_28 | AC | 1,052 ms
97,628 KB |
testcase_29 | AC | 1,261 ms
104,260 KB |
testcase_30 | AC | 785 ms
90,804 KB |
testcase_31 | AC | 1,232 ms
102,788 KB |
testcase_32 | AC | 902 ms
91,084 KB |
testcase_33 | AC | 1,160 ms
98,540 KB |
testcase_34 | AC | 1,395 ms
108,500 KB |
testcase_35 | AC | 1,348 ms
106,120 KB |
testcase_36 | AC | 913 ms
90,660 KB |
testcase_37 | AC | 1,134 ms
98,024 KB |
testcase_38 | AC | 940 ms
91,028 KB |
testcase_39 | AC | 953 ms
93,376 KB |
testcase_40 | AC | 1,347 ms
106,528 KB |
testcase_41 | AC | 875 ms
91,064 KB |
testcase_42 | AC | 1,184 ms
102,788 KB |
testcase_43 | AC | 1,155 ms
98,444 KB |
testcase_44 | AC | 1,206 ms
108,040 KB |
testcase_45 | AC | 1,257 ms
108,960 KB |
testcase_46 | AC | 1,235 ms
109,044 KB |
ソースコード
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