提出 #29479636


ソースコード 拡げる

def no!; p(-1); exit end

(N,M),D,*AB = $<.map{|ln| ln.split.map(&:to_i) }
D.unshift 0

G = [-1]*(N+1)
F = lambda{|a|
	G[a]<0 ? a : G[a] = F[G[a]]
}
U = lambda{|a,b|
	a,b = F[a],F[b]
	next true if a==b
	a,b = b,a if G[b]<G[a]
	G[a] += G[b]
	G[b] = a
	next false
}
AB.each{|a,b|
	no! if 0>D[a] -= 1
	no! if 0>D[b] -= 1
	no! if U[a,b]
}

Vg = (1..N).group_by(&F).map{|_,as| as.flat_map{|a| [a]*D[a] } }
Vg.sort_by!(&:size)
no! if Vg.size!=N-M || Vg[0].empty?

A = []
while as = Vg.pop
	bs = Vg[0]
	no! if ! bs || bs.size!=1
	A<<"#{as.pop} #{bs.pop}"
	next Vg.shift if as.empty?

	while 1<as.size
		Vg.shift
		bs = Vg[0]
		no! if ! bs || bs.size!=1
		A<<"#{as.pop} #{bs.pop}"
	end
	Vg[0] = as
end

puts A

提出情報

提出日時
問題 F - Construct Highway
ユーザ ds14050
言語 Ruby (2.7.1)
得点 0
コード長 746 Byte
結果 TLE
実行時間 2208 ms
メモリ 72224 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 500
結果
AC × 3
AC × 22
TLE × 7
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, hand_02.txt, hand_03.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, sample_01.txt, sample_02.txt, sample_03.txt
ケース名 結果 実行時間 メモリ
hand_01.txt AC 57 ms 14216 KiB
hand_02.txt AC 57 ms 14116 KiB
hand_03.txt AC 55 ms 14264 KiB
random_01.txt AC 442 ms 50884 KiB
random_02.txt AC 445 ms 49132 KiB
random_03.txt AC 436 ms 41968 KiB
random_04.txt AC 443 ms 55460 KiB
random_05.txt AC 418 ms 48624 KiB
random_06.txt AC 382 ms 38760 KiB
random_07.txt TLE 2207 ms 54352 KiB
random_08.txt AC 846 ms 50840 KiB
random_09.txt AC 426 ms 40080 KiB
random_10.txt AC 1973 ms 57084 KiB
random_11.txt AC 693 ms 50584 KiB
random_12.txt AC 430 ms 39768 KiB
random_13.txt TLE 2208 ms 66240 KiB
random_14.txt AC 416 ms 37436 KiB
random_15.txt TLE 2208 ms 72004 KiB
random_16.txt TLE 2207 ms 65688 KiB
random_17.txt TLE 2208 ms 72224 KiB
random_18.txt TLE 2208 ms 72168 KiB
random_19.txt TLE 2207 ms 64248 KiB
random_20.txt AC 309 ms 59684 KiB
random_21.txt AC 349 ms 35716 KiB
random_22.txt AC 101 ms 28440 KiB
random_23.txt AC 102 ms 28608 KiB
sample_01.txt AC 57 ms 14128 KiB
sample_02.txt AC 55 ms 14132 KiB
sample_03.txt AC 57 ms 14116 KiB