Submission #18036165


Source Code Expand

class Array
	def ^ other
		return self + other - (self&other)
	end
end

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

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

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

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

子を認知する親の行列 = [p=N]
子を認知する親の行列.concat ChildrenOfV[p].each{|c|
	ParentOfV[c] = ChildrenOfV[c].delete p
} while p = 子を認知する親の行列.shift

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

上方の辺の色を決定する頂点の行列 = *1...N
while p = 上方の辺の色を決定する頂点の行列.shift
	next if ColorOfEdge[EdgeOfV[p]]
	next 上方の辺の色を決定する頂点の行列.push(p) if ChildrenOfV[p].any?{|c| ColorOfEdge[EdgeOfV[c]].nil? }
	begin
		ColorOfV[p] = ChildrenOfV[p].map{|u| ColorOfV[u] }.inject(ColorOfV[p],:^)
		ColorOfEdge[EdgeOfV[p]] = Colors[ColorOfV[p].max||-1]
		p = ParentOfV[p]
	end while p < N && ChildrenOfV[p].all?{|c| ColorOfEdge[EdgeOfV[c]] }
end

puts ColorOfEdge

Submission Info

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

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 6
Status
AC × 2
AC × 23
TLE × 9
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 63 ms 14220 KiB
random_02.txt AC 1021 ms 155500 KiB
random_03.txt AC 482 ms 66848 KiB
random_04.txt AC 119 ms 26264 KiB
random_05.txt AC 281 ms 39712 KiB
random_06.txt AC 84 ms 16012 KiB
random_07.txt AC 637 ms 116092 KiB
random_08.txt AC 275 ms 46640 KiB
random_09.txt AC 622 ms 105780 KiB
random_10.txt AC 504 ms 89000 KiB
random_11.txt AC 59 ms 14272 KiB
random_12.txt AC 840 ms 122084 KiB
random_13.txt AC 384 ms 42604 KiB
random_14.txt AC 711 ms 111800 KiB
random_15.txt AC 522 ms 86856 KiB
random_16.txt AC 328 ms 34276 KiB
random_17.txt TLE 4444 ms 1189860 KiB
random_18.txt AC 640 ms 232092 KiB
random_19.txt TLE 4445 ms 1224160 KiB
random_20.txt TLE 4437 ms 919208 KiB
random_21.txt AC 135 ms 19864 KiB
random_22.txt TLE 4427 ms 494372 KiB
random_23.txt AC 3337 ms 635864 KiB
random_24.txt AC 799 ms 233876 KiB
random_25.txt TLE 4436 ms 867904 KiB
random_26.txt AC 178 ms 24700 KiB
random_27.txt TLE 4414 ms 108508 KiB
random_28.txt TLE 4417 ms 157328 KiB
random_29.txt TLE 4418 ms 194832 KiB
random_30.txt TLE 4416 ms 184988 KiB
sample_01.txt AC 62 ms 14272 KiB
sample_02.txt AC 58 ms 14212 KiB