結果

問題 No.2290 UnUnion Find
ユーザー maguroflymagurofly
提出日時 2023-05-05 21:51:28
言語 Ruby
(3.3.0)
結果
AC  
実行時間 777 ms / 2,000 ms
コード長 773 bytes
コンパイル時間 378 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 46,720 KB
最終ジャッジ日時 2024-05-02 15:58:07
合計ジャッジ時間 33,109 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
12,288 KB
testcase_01 AC 72 ms
12,288 KB
testcase_02 AC 474 ms
12,544 KB
testcase_03 AC 565 ms
26,624 KB
testcase_04 AC 581 ms
26,752 KB
testcase_05 AC 590 ms
26,624 KB
testcase_06 AC 580 ms
26,624 KB
testcase_07 AC 582 ms
26,624 KB
testcase_08 AC 583 ms
26,752 KB
testcase_09 AC 596 ms
26,752 KB
testcase_10 AC 603 ms
26,752 KB
testcase_11 AC 583 ms
26,880 KB
testcase_12 AC 592 ms
26,624 KB
testcase_13 AC 587 ms
26,752 KB
testcase_14 AC 573 ms
26,624 KB
testcase_15 AC 576 ms
26,624 KB
testcase_16 AC 576 ms
26,880 KB
testcase_17 AC 582 ms
26,752 KB
testcase_18 AC 578 ms
26,624 KB
testcase_19 AC 641 ms
32,512 KB
testcase_20 AC 761 ms
46,720 KB
testcase_21 AC 470 ms
13,568 KB
testcase_22 AC 548 ms
21,120 KB
testcase_23 AC 540 ms
20,992 KB
testcase_24 AC 680 ms
36,736 KB
testcase_25 AC 529 ms
18,432 KB
testcase_26 AC 734 ms
39,936 KB
testcase_27 AC 726 ms
38,016 KB
testcase_28 AC 592 ms
26,752 KB
testcase_29 AC 709 ms
35,968 KB
testcase_30 AC 498 ms
14,464 KB
testcase_31 AC 677 ms
34,304 KB
testcase_32 AC 545 ms
18,944 KB
testcase_33 AC 588 ms
26,368 KB
testcase_34 AC 777 ms
46,592 KB
testcase_35 AC 747 ms
39,552 KB
testcase_36 AC 539 ms
19,456 KB
testcase_37 AC 563 ms
24,576 KB
testcase_38 AC 531 ms
19,328 KB
testcase_39 AC 560 ms
21,632 KB
testcase_40 AC 758 ms
38,656 KB
testcase_41 AC 529 ms
17,024 KB
testcase_42 AC 673 ms
32,384 KB
testcase_43 AC 614 ms
30,720 KB
testcase_44 AC 583 ms
26,880 KB
testcase_45 AC 599 ms
26,752 KB
testcase_46 AC 632 ms
26,880 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:31: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

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
0