首页 > 代码库 > 2014西安网络预选赛1009(点分治)HDU5016
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?
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 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.
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.
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
然后算的时候只需要用总的减去该条分支的即可
<script src="https://code.csdn.net/snippets/474486.js" type="text/javascript"></script>
2014西安网络预选赛1009(点分治)HDU5016
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。