「SPOJ1825」Free tour II

2015年1月1日5,1523

After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, Travel Agent SPOJ goes on with another discount tour.

The tour will be held on ICPC island, a miraculous one on the Pacific Ocean. We list N places (indexed from 1 to N) where the visitors can have a trip. Each road connecting them has an interest value, and this value can be negative (if there is nothing interesting to view there). Simply, these N places along with the roads connecting them form a tree structure. We will choose two places as the departure and destination of the tour.

Since September is the festival season of local inhabitants, some places are extremely crowded (we call themcrowded places). Therefore, the organizer of the excursion hopes the tour will visit at most K crowded places(too tiring to visit many of them) and of course, the total number of interesting value should be maximum.

Briefly, you are given a map of N places, an integer K, and M id numbers of crowded place. Please help us to find the optimal tour. Note that we can visit each place only once (or our customers easily feel bored), also the departure and destination places don’t need to be different.

Input

There is exactly one case. First one line, containing 3 integers N K M, with 1 <= N <= 200000, 0 <= K <=M, 0 <= M <= N.

Next M lines, each line includes an id number of a crowded place.

The last (N – 1) lines describe (N – 1) two-way roads connected N places, form a b i, with a, b is the id of 2 places, and i is its interest value (-10000 <= i <= 10000).

Output

Only one number, the maximum total interest value we can obtain.

Example

题解

设路径起点为根,终点为x的路径,经过黑色结点数量为deep[x],路径长度为dis[x]
考虑解决经过根的路径,递归子树处理其它路径。
依次处理root的每棵子树,处理到子树S的时候,我们需要知道出发点为根,终点在前S-1棵子树子树中,经过t(t<K)个黑点的路径的最长长度,记为mx[t]
则对于子树S的结点x\[mx[t]+dis[x](deep[x]+t\leq K)\xrightarrow{update} ans\](若根为黑色处理时将K–,处理完恢复)若按照deep倒序处理每个结点,mx指针now按照升序扫,则\(mx[now-1]\xrightarrow{update} mx[now]\),这样就能得到符合条件的mx[t]的最大值。
然后再考虑用子树S的信息更新mx,将两个数组合并的操作次数是\(max(L1,L2)\),可以考虑按照每棵子树deep的升序依次合并子树。从总体来看,由于总边数是n-1,则排序的复杂度nlogn,同时这样启发式合并使得合并的复杂度降为nlogn

 

avatar
2 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
lihshzweraa Recent comment authors
  Subscribe  
提醒
lihs
lihs

那个应该是t+deep <=k 吧

aa
aa

试一下这个
2 0 0
1 2 1