Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS換根 || 樹形dp】
傳送門:http://codeforces.com/contest/1092/problem/F
F. Tree with Maximum Cost
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard outputYou are given a tree consisting exactly of nn vertices. Tree is a connected undirected graph with
Let dist(x,y)dist(x,y) be the distance between the vertices xx and yy. The distance between the vertices is the number of edges on the simple path between them.
Let's define the cost of the tree as the following value: firstly, let's fix some vertex of the tree. Let it be vv. Then the cost of the tree is ∑i=1ndist(i,v)⋅ai∑i=1ndist(i,v)⋅ai.
Your task is to calculate the maximum possible cost of the tree if you can choose