Submission #49587091


Source Code Expand

class BIT
	def initialize n
		@a = [0]*(n+1) # 1-indexed internally
	end

	def [] i
		i += 1
		s = 0
		while 0<i
			s += @a[i]
			i &= i-1
		end
		return s
	end

	def add i,d
		i += 1
		while i<@a.size
			@a[i] += d
			i += i&-i
		end
	end
	def inc i; add i,1 end
end

N = gets.to_i
P = [0]+[nil]*N
Dn = BIT.new N+1
DnC = Array.new(N+1){{}}
DnSum = [0]*(N+1)
(N-1).times{
	u,v = gets.split.map(&:to_i)
	DnC[u][v] = v
	DnC[v][u] = u
}
F = lambda{|v,p|
	P[v] = p
	cs = DnC[v]
	dnp = v-1
	cs.transform_values!{|c|
		next if c==p
		dnc = Dn[v]
		F[c,v]
		dnc = Dn[v]-dnc
		DnSum[v] += dnc+DnSum[c]
		dnp -= dnc
		next dnc
	}
	cs[p] = dnp
	Dn.inc v
}
G = lambda{|v,dnsump|
	p = P[v]
	DnSum[v] += DnC[v][p]+dnsump
	DnC[v].each{|c,dnc|
		G[c,DnSum[v]-dnc-DnSum[c]] if c!=p
	}
}

F[1,0]
G[1,0]

puts DnSum[1,N]*' '

Submission Info

Submission Time
Task G - Tree Inversion
User ds14050
Language Ruby (ruby 3.2.2)
Score 600
Code Size 870 Byte
Status AC
Exec Time 913 ms
Memory 369924 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 72
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 01_random_53.txt, 01_random_54.txt, 01_random_55.txt, 01_random_56.txt, 01_random_57.txt, 01_random_58.txt, 01_random_59.txt, 01_random_60.txt, 01_random_61.txt, 01_random_62.txt, 01_random_63.txt, 02_handmade_64.txt, 02_handmade_65.txt, 02_handmade_66.txt, 02_handmade_67.txt, 02_handmade_68.txt, 02_handmade_69.txt, 02_handmade_70.txt, 02_handmade_71.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 128 ms 17224 KiB
00_sample_01.txt AC 43 ms 17240 KiB
00_sample_02.txt AC 45 ms 17444 KiB
01_random_03.txt AC 539 ms 74440 KiB
01_random_04.txt AC 871 ms 338116 KiB
01_random_05.txt AC 611 ms 67992 KiB
01_random_06.txt AC 590 ms 68408 KiB
01_random_07.txt AC 576 ms 74460 KiB
01_random_08.txt AC 868 ms 247856 KiB
01_random_09.txt AC 650 ms 68108 KiB
01_random_10.txt AC 656 ms 67800 KiB
01_random_11.txt AC 598 ms 74240 KiB
01_random_12.txt AC 892 ms 336256 KiB
01_random_13.txt AC 621 ms 67904 KiB
01_random_14.txt AC 661 ms 68412 KiB
01_random_15.txt AC 526 ms 74284 KiB
01_random_16.txt AC 839 ms 255320 KiB
01_random_17.txt AC 616 ms 67848 KiB
01_random_18.txt AC 613 ms 68316 KiB
01_random_19.txt AC 531 ms 74516 KiB
01_random_20.txt AC 865 ms 334508 KiB
01_random_21.txt AC 597 ms 67820 KiB
01_random_22.txt AC 571 ms 68588 KiB
01_random_23.txt AC 538 ms 74324 KiB
01_random_24.txt AC 913 ms 309156 KiB
01_random_25.txt AC 652 ms 67916 KiB
01_random_26.txt AC 618 ms 68244 KiB
01_random_27.txt AC 580 ms 74404 KiB
01_random_28.txt AC 856 ms 307416 KiB
01_random_29.txt AC 628 ms 67824 KiB
01_random_30.txt AC 599 ms 68428 KiB
01_random_31.txt AC 60 ms 18908 KiB
01_random_32.txt AC 206 ms 94960 KiB
01_random_33.txt AC 362 ms 47364 KiB
01_random_34.txt AC 621 ms 65708 KiB
01_random_35.txt AC 320 ms 50152 KiB
01_random_36.txt AC 563 ms 65936 KiB
01_random_37.txt AC 98 ms 22040 KiB
01_random_38.txt AC 239 ms 40104 KiB
01_random_39.txt AC 123 ms 43212 KiB
01_random_40.txt AC 166 ms 29276 KiB
01_random_41.txt AC 596 ms 61464 KiB
01_random_42.txt AC 207 ms 37824 KiB
01_random_43.txt AC 352 ms 47448 KiB
01_random_44.txt AC 610 ms 65720 KiB
01_random_45.txt AC 127 ms 27436 KiB
01_random_46.txt AC 347 ms 105852 KiB
01_random_47.txt AC 65 ms 19196 KiB
01_random_48.txt AC 541 ms 65260 KiB
01_random_49.txt AC 338 ms 51532 KiB
01_random_50.txt AC 341 ms 48272 KiB
01_random_51.txt AC 271 ms 37816 KiB
01_random_52.txt AC 565 ms 74180 KiB
01_random_53.txt AC 852 ms 313652 KiB
01_random_54.txt AC 639 ms 68076 KiB
01_random_55.txt AC 608 ms 68808 KiB
01_random_56.txt AC 564 ms 74308 KiB
01_random_57.txt AC 889 ms 255348 KiB
01_random_58.txt AC 634 ms 67140 KiB
01_random_59.txt AC 621 ms 68340 KiB
01_random_60.txt AC 601 ms 74236 KiB
01_random_61.txt AC 905 ms 336320 KiB
01_random_62.txt AC 682 ms 68088 KiB
01_random_63.txt AC 651 ms 68372 KiB
02_handmade_64.txt AC 44 ms 17132 KiB
02_handmade_65.txt AC 44 ms 17232 KiB
02_handmade_66.txt AC 725 ms 369108 KiB
02_handmade_67.txt AC 717 ms 369924 KiB
02_handmade_68.txt AC 715 ms 369916 KiB
02_handmade_69.txt AC 704 ms 369868 KiB
02_handmade_70.txt AC 716 ms 369848 KiB
02_handmade_71.txt AC 736 ms 369788 KiB