2014西安网络预选赛1009(点分治)HDU5016

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 Description
Trader 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 could support him to build only  one new marts, he wants to attract as many 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. Could you help him?
 

Input
There 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 b i, e i and w i, (1 ≤ b i,e i ≤ n,1 ≤ w i ≤ 10000), indicates that there’s one road connecting city b i and e i, and its length is w i.

Last line : n(1 ≤ n ≤ 10 5) 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.
 

Output
For each test case, output one number, denotes the number of people you can attract, taking district as a unit.
 

Sample Input
    
    
    
    
5 1 2 1 2 3 1 3 4 1 4 5 1 1 0 0 0 1 5 1 2 1 2 3 1 3 4 1 4 5 1 1 0 0 0 0 1 1 1 0
 

Sample Output
    
    
    
    
2 4 0 1

题意:给出n个点的树,有些点已经有了市场,而每个点都会被它最近的市场统治,现在要找一个点建市场,使得能统治最多的点

思路:又是一道树分治类型的题目,这种题目一直是难点也是热点吧

            首先要预处理,即求出每个点被哪些市场统治了,可以用pair来存,代码里为du[]数组,第一维是距离,第二维是被哪个点统治,这个用树形DP很容易求得

            然后就是树分治的部分

            对于每一个分治中心C,要求的就是点u能不能通过C被v统治

            可以先一遍DFS扫一遍所有点,然后如果du[u]>=dis[u]则将该点加入vector,其中dis[u]是u与分治中心C的距离

            然后就可以一个分支一个分支的求了,对一个分支里面的每个点可以通过在vector上二分找到能统治的所有点的数量

            但是会算重复,因为它会将该条分支满足的点也算进去

            所以在求之前要先在这条分支上扫一遍所有点,将满足上述公式的点加入另一个vector

            然后算的时候只需要用总的减去该条分支的即可

你可能感兴趣的:(ACM,HDU)