Submission #52179331


Source Code Expand

Copy
N = gets.to_i
E = Array.new(N+1){[]}
(N-1).times{
a,b = gets.split.map(&:to_i)
E[a]<<b
E[b]<<a
}
C = 0,*gets.split.map(&:to_i)
S = [0]*(N+1)
F = lambda{|a,p|
E[a].each{|b|
next if b==p
c,s = F[b,a]
C[a] += c
S[a] += s
}
next C[a],S[a]+C[a]
}
C[0],S[0] = F[1,0]
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
N = gets.to_i
E = Array.new(N+1){[]}
(N-1).times{
	a,b = gets.split.map(&:to_i)
	E[a]<<b
	E[b]<<a
}
C = 0,*gets.split.map(&:to_i)
S = [0]*(N+1)

F = lambda{|a,p|
	E[a].each{|b|
		next if b==p
		c,s = F[b,a]
		C[a] += c
		S[a] += s
	}
	next C[a],S[a]+C[a]
}
C[0],S[0] = F[1,0]

G = lambda{|a,p|
	c,s = C[a],S[a]
	pc,ps = C[p]-c,S[p]-s-c
	C[a] += pc
	S[a] += ps+pc
	E[a].each{|b|
		next if b==p
		G[b,a]
	}
}
G[1,0]

p S.min

Submission Info

Submission Time
Task E - Minimize Sum of Distances
User ds14050
Language Ruby (ruby 3.2.2)
Score 475
Code Size 457 Byte
Status AC
Exec Time 271 ms
Memory 151960 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 475 / 475
Status
AC × 3
AC × 31
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt
All example0.txt, example1.txt, example2.txt, test_00.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt
Case Name Status Exec Time Memory
example0.txt AC 130 ms 17160 KB
example1.txt AC 42 ms 17272 KB
example2.txt AC 42 ms 17104 KB
test_00.txt AC 141 ms 29188 KB
test_01.txt AC 109 ms 24708 KB
test_02.txt AC 156 ms 30724 KB
test_03.txt AC 73 ms 19876 KB
test_04.txt AC 145 ms 29552 KB
test_05.txt AC 106 ms 24356 KB
test_06.txt AC 139 ms 61060 KB
test_07.txt AC 142 ms 73744 KB
test_08.txt AC 113 ms 50796 KB
test_09.txt AC 206 ms 36440 KB
test_10.txt AC 197 ms 35568 KB
test_11.txt AC 198 ms 36172 KB
test_12.txt AC 196 ms 35892 KB
test_13.txt AC 203 ms 36716 KB
test_14.txt AC 204 ms 35644 KB
test_15.txt AC 185 ms 35608 KB
test_16.txt AC 185 ms 34484 KB
test_17.txt AC 203 ms 35368 KB
test_18.txt AC 193 ms 34532 KB
test_19.txt AC 206 ms 35616 KB
test_20.txt AC 185 ms 34484 KB
test_21.txt AC 270 ms 149428 KB
test_22.txt AC 271 ms 151960 KB
test_23.txt AC 265 ms 122352 KB
test_24.txt AC 256 ms 109672 KB
test_25.txt AC 264 ms 129236 KB
test_26.txt AC 270 ms 135072 KB
test_27.txt AC 43 ms 17068 KB


2025-04-15 (Tue)
11:54:58 +00:00