Submission #53927502


Source Code Expand

module UF
	def find a
		self[a]<0 ? a : self[a] = find(self[a])
	end

	def unite a,b
		a = find a
		b = find b
		return if a==b
		a,b = b,a if self[b]<self[a]
		self[a] += self[b]
		self[b] = a
		return true
	end
end

N,Q = gets.split.map(&:to_i)
G = Array.new(11){ [-1]*(N+1) }.each{|g| g.extend UF }
sc = (N-1).times.sum{
	a,b,c = gets.split.map(&:to_i)
	G[c..10].each{|g|
		g.unite a,b
	}
	next c
}
puts Q.times.map{
	u,v,w = gets.split.map(&:to_i)
	if G[w].unite u,v
		sc += w
		sc -= (w+1..10).find{|c| ! G[c].unite u,v }
	end
	next sc
}

Submission Info

Submission Time
Task F - MST Query
User ds14050
Language Ruby (ruby 3.2.2)
Score 550
Code Size 576 Byte
Status AC
Exec Time 1512 ms
Memory 37396 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 550 / 550
Status
AC × 2
AC × 44
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 01_small_05.txt, 01_small_06.txt, 01_small_07.txt, 01_small_08.txt, 01_small_09.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt, 02_random_20.txt, 02_random_21.txt, 02_random_22.txt, 02_random_23.txt, 02_random_24.txt, 02_random_25.txt, 02_random_26.txt, 02_random_27.txt, 02_random_28.txt, 02_random_29.txt, 02_random_30.txt, 02_random_31.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 57 ms 17192 KiB
00_sample_01.txt AC 47 ms 17456 KiB
01_small_00.txt AC 47 ms 17412 KiB
01_small_01.txt AC 50 ms 17484 KiB
01_small_02.txt AC 53 ms 17408 KiB
01_small_03.txt AC 50 ms 17432 KiB
01_small_04.txt AC 51 ms 17592 KiB
01_small_05.txt AC 351 ms 19260 KiB
01_small_06.txt AC 350 ms 19276 KiB
01_small_07.txt AC 348 ms 19400 KiB
01_small_08.txt AC 355 ms 19300 KiB
01_small_09.txt AC 351 ms 19396 KiB
02_random_00.txt AC 762 ms 27564 KiB
02_random_01.txt AC 1410 ms 36368 KiB
02_random_02.txt AC 970 ms 31072 KiB
02_random_03.txt AC 1411 ms 36468 KiB
02_random_04.txt AC 1063 ms 30228 KiB
02_random_05.txt AC 1414 ms 36628 KiB
02_random_06.txt AC 1053 ms 32032 KiB
02_random_07.txt AC 1426 ms 36512 KiB
02_random_08.txt AC 1334 ms 35396 KiB
02_random_09.txt AC 1450 ms 37396 KiB
02_random_10.txt AC 861 ms 27952 KiB
02_random_11.txt AC 1396 ms 36480 KiB
02_random_12.txt AC 668 ms 27664 KiB
02_random_13.txt AC 1302 ms 36416 KiB
02_random_14.txt AC 1242 ms 33808 KiB
02_random_15.txt AC 1512 ms 36632 KiB
02_random_16.txt AC 730 ms 27864 KiB
02_random_17.txt AC 1179 ms 37340 KiB
02_random_18.txt AC 1115 ms 35596 KiB
02_random_19.txt AC 1310 ms 36408 KiB
02_random_20.txt AC 870 ms 27932 KiB
02_random_21.txt AC 1263 ms 37328 KiB
02_random_22.txt AC 911 ms 28672 KiB
02_random_23.txt AC 1243 ms 36240 KiB
02_random_24.txt AC 878 ms 30216 KiB
02_random_25.txt AC 1338 ms 36184 KiB
02_random_26.txt AC 902 ms 29888 KiB
02_random_27.txt AC 1274 ms 36352 KiB
02_random_28.txt AC 1212 ms 34372 KiB
02_random_29.txt AC 1238 ms 35708 KiB
02_random_30.txt AC 757 ms 28416 KiB
02_random_31.txt AC 1321 ms 36168 KiB