Submission #18059514


Source Code Expand

class Array
	def ^ other # Both of self and other must be unique and sorted.
		i,v,*a = j = t = 0
		ix,jx = self.size,other.size
		begin
			(i+=1; j+=1) while (v = other[j]) && self[i] == v
			a.concat self[i...i+=t] if 0 < t = v ? self[i,ix].bsearch_index{ v<=_1 }||ix-i : ix-i
			a.concat other[j...j+=t] if 0 < t = (v = self[i]) ? other[j,jx].bsearch_index{ v<=_1 }||jx-j : jx-j
		end while v
		return a
	end
end

N,Q = gets.split.map(&:to_i)

ChildrenOfV = Array.new(N+1){ {} }
ParentOfV = [nil]*(N+1)
ColorsOfV = Array.new(N+1){ [] }
EdgeOfV = [nil]*(N+1)
ColorOfE = [nil]*(N-1)

Edges = (N-1).times.map{
	a,b = gets.split.map(&:to_i)
	ChildrenOfV[a][b] = b
	ChildrenOfV[b][a] = a
	next a,b
}

ET,L = [],[nil]*(N+1)
子を認知する親の行列 = [N]
while p = 子を認知する親の行列.pop
	L[p] = ET.size
	ET << p
	子を認知する親の行列.concat ChildrenOfV[p].keys.each{|c|
		ParentOfV[c] = ChildrenOfV[c].delete p
	}
end

Colors = Q.times.map{|z|
	u,v,c = gets.split.map(&:to_i)
	ColorsOfV[u] << z
	ColorsOfV[v] << z
	next c
}
Colors << 0

Edges.each_with_index{|(p,c),i|
	EdgeOfV[ParentOfV[c] == p ? c : p] = i
}

子の色リストスタック = []
ET.shift
ET.reverse_each{|p|
	cn = ChildrenOfV[p].size
	子の色リストスタック.push (0 < cn ? 子の色リストスタック.pop(cn).delete_if(&:empty?).inject(ColorsOfV[p],:^) : ColorsOfV[p])
	ColorOfE[EdgeOfV[p]] = Colors[子の色リストスタック[-1][-1]||-1]
}

Kernel.p *ColorOfE

Submission Info

Submission Time
Task M - 筆塗り
User ds14050
Language Ruby (2.7.1)
Score 0
Code Size 1531 Byte
Status TLE
Exec Time 4418 ms
Memory 201508 KiB

Compile Error

./Main.rb:59: warning: `*' interpreted as argument prefix

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 6
Status
AC × 2
AC × 30
TLE × 2
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All 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, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, sample_01.txt, sample_02.txt
Case Name Status Exec Time Memory
random_01.txt AC 64 ms 14296 KiB
random_02.txt AC 1397 ms 73324 KiB
random_03.txt AC 531 ms 59468 KiB
random_04.txt AC 148 ms 19200 KiB
random_05.txt AC 299 ms 42292 KiB
random_06.txt AC 78 ms 16932 KiB
random_07.txt AC 987 ms 40268 KiB
random_08.txt AC 317 ms 33656 KiB
random_09.txt AC 774 ms 62240 KiB
random_10.txt AC 802 ms 39980 KiB
random_11.txt AC 61 ms 14164 KiB
random_12.txt AC 1450 ms 68124 KiB
random_13.txt AC 421 ms 55576 KiB
random_14.txt AC 1272 ms 54452 KiB
random_15.txt AC 929 ms 47472 KiB
random_16.txt AC 329 ms 43132 KiB
random_17.txt TLE 4418 ms 201508 KiB
random_18.txt AC 281 ms 80728 KiB
random_19.txt TLE 4418 ms 192640 KiB
random_20.txt AC 696 ms 95336 KiB
random_21.txt AC 130 ms 23036 KiB
random_22.txt AC 868 ms 69132 KiB
random_23.txt AC 422 ms 122648 KiB
random_24.txt AC 283 ms 94140 KiB
random_25.txt AC 1102 ms 200436 KiB
random_26.txt AC 171 ms 32556 KiB
random_27.txt AC 2882 ms 190776 KiB
random_28.txt AC 3775 ms 196548 KiB
random_29.txt AC 1006 ms 163500 KiB
random_30.txt AC 1462 ms 195304 KiB
sample_01.txt AC 60 ms 14348 KiB
sample_02.txt AC 61 ms 14280 KiB