結果
| 問題 |
No.2290 UnUnion Find
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-05-05 21:51:28 |
| 言語 | Ruby (3.4.1) |
| 結果 |
AC
|
| 実行時間 | 957 ms / 2,000 ms |
| コード長 | 773 bytes |
| コンパイル時間 | 618 ms |
| コンパイル使用メモリ | 7,296 KB |
| 実行使用メモリ | 46,464 KB |
| 最終ジャッジ日時 | 2024-11-23 07:02:21 |
| 合計ジャッジ時間 | 39,755 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 46 |
コンパイルメッセージ
Main.rb:31: warning: ambiguous first argument; put parentheses or a space even after `-' operator Syntax OK
ソースコード
N, Q = gets.split.map(&:to_i)
Component = Struct.new(:id, :vertices, :next, :prev)
@component_of = Array.new(N) { |i| Component.new(i, [i], nil) }
N.times do |i|
@component_of[i].next = @component_of[(i + 1) % N]
@component_of[i].prev = @component_of[(i - 1) % N]
end
def merge(u, v)
x, y = @component_of[u], @component_of[v]
return false if x.id == y.id
x, y = y, x if x.vertices.size < y.vertices.size
# merge y to x
y.vertices.each do |v|
x.vertices << v
@component_of[v] = x
end
# delete y
y.next.prev, y.prev.next = y.prev, y.next
true
end
Q.times do
t, *q = gets.split.map(&:to_i)
case t
when 1
merge(q[0] - 1, q[1] - 1)
when 2
x = @component_of[q[0] - 1]
if x.next.id == x.id
puts -1
else
puts x.next.vertices[0] + 1
end
end
end