Mart master II
Time Limit: 12000/6000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 167 accepted submission (s): 63
Problem descriptiontrader dogy lives in city S, which consists of N districts. there are n-1 bidirectional roads in city s, each connects a pair of districts. indeed, city s is connected, I. e. people can travel between every pair of districts by roads.
In some districts there are marts founded by dogy's competitors. when people go to marts, they'll choose the nearest one. in cases there are more than one nearest marts, they'll choose the one with minimal city number.
Dogy's money cocould support him to build only
OneNew marts, he wants to attract as your people as possible, that is, to build his Marts in some way that maximize the number of people who will choose his mart as favorite. cocould you help him?
Inputthere are multiple test cases. Please process till EOF.
In each test case:
First line: an integer n indicating the number of districts.
Next n-1 lines: Each contains three numbers Bi, EI and WI, (1 ≤ Bi, EI ≤ n, 1 ≤ wi ≤ 10000 ), indicates that there's one road connecting city Bi and ei, and its length is WI.
Last line: n (1 ≤ n ≤ 105) numbers, each number is either 0 or 1, i-th number is 1 indicates that the I-th district has Mart in the beginning and vice versa.
Outputfor each test case, output one number, denotes the number of people you can attract, taking district as a unit.
Sample Input
51 2 12 3 13 4 14 5 11 0 0 0 151 2 12 3 13 4 14 5 11 0 0 0 01110
Sample output
2401
Meaning: tree with n vertices. Some vertices already have a market, and each vertex will be ruled by its recent market. Now we need to create a market for a vertex, vertices that can govern the most
Train of Thought: it is also a topic of tree governance type. This topic has always been a difficult and hot topic.
First, we need to pre-process the data, that is, find the market rule for each vertex. We can use pair to store the data. The data in the Code is an array of Du []. The first dimension is the distance, and the second dimension is the rule of which point, this can be easily obtained using the tree-like DP.
Then there is the sub-governance part of the tree.
For each sub-governance center C, the requirement is whether point u can be ruled by V through C.
You can scan all vertices at DFS first, and then add the points to the vector if du [u]> = dis [u, where dis [u] is the distance between U and sub-governance center C.
Then we can find the number of vertices that can be ruled by the Second Division on the vector.
But it will calculate the repetition, because it will also calculate the points satisfied by the branch.
Therefore, we need to scan all vertices on this branch before seeking, and add the vertices that meet the preceding formula to another vector.
Then, you only need to subtract the total number of the branches.
2014 Xi'an network qualifying round 1009 (point Division) hdu5016