結果
問題 | No.2290 UnUnion Find |
ユーザー | 👑 箱 |
提出日時 | 2023-03-05 23:45:12 |
言語 | Kotlin (1.9.23) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,311 bytes |
コンパイル時間 | 18,016 ms |
コンパイル使用メモリ | 447,280 KB |
実行使用メモリ | 82,200 KB |
最終ジャッジ日時 | 2023-10-24 06:44:14 |
合計ジャッジ時間 | 45,526 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 313 ms
56,524 KB |
testcase_01 | AC | 315 ms
56,512 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | AC | 1,130 ms
82,048 KB |
testcase_05 | AC | 1,138 ms
82,040 KB |
testcase_06 | AC | 1,125 ms
82,008 KB |
testcase_07 | AC | 1,141 ms
82,004 KB |
testcase_08 | AC | 1,139 ms
81,984 KB |
testcase_09 | AC | 1,124 ms
82,064 KB |
testcase_10 | AC | 1,130 ms
81,972 KB |
testcase_11 | AC | 1,130 ms
81,976 KB |
testcase_12 | AC | 1,118 ms
81,972 KB |
testcase_13 | AC | 1,108 ms
82,008 KB |
testcase_14 | AC | 1,095 ms
82,004 KB |
testcase_15 | AC | 1,088 ms
82,004 KB |
testcase_16 | AC | 1,085 ms
81,996 KB |
testcase_17 | AC | 1,102 ms
81,676 KB |
testcase_18 | AC | 1,089 ms
82,008 KB |
testcase_19 | TLE | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
testcase_45 | -- | - |
testcase_46 | -- | - |
ソースコード
import java.io.PrintWriter import java.util.* import kotlin.math.* fun PrintWriter.solve() { val n = nextInt() val q = nextInt() val parents = mutableSetOf<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 (v != v1) { println(v1 + 1) } else if (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