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 them crowded 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

Input:
8 2 3
3
5
7
1 3 1
2 3 10
3 4 -2
4 5 -1
5 7 6
5 6 5
4 8 3 Output:
12

Explanation

We choose 2 and 6 as the departure and destination place, so the tour will be 2 -> 3 -> 4 -> 5 -> 6, total interest value = 10 + (-2) + (-1) + 5 = 12
* Added some unofficial case

题解:

设路径起点为根,终点为x的路径,经过黑色结点数量为deep[x],路径长度为dis[x]
考虑解决经过根的路径,递归子树处理其它路径。
依次处理root的每棵子树,处理到子树S的时候,我们需要知道出发点为根,终点在前S-1棵子树子树中,经过t(t<K)个黑点的路径的最长长度,记为mx[t]
则对于子树S的结点x

    mx[t]+dis[x](deep[x]+t<=k)-->ans

mx[t]+dis[x](deep[x]+t≤K)−→−−−updateans


(若根为黑色处理时将K–,处理完恢复)若按照deep倒序处理每个结点,mx指针now按照升序扫,mx[now-1]---> mx[now]mx[now−1]−→−−−updatemx[now]

这样就能得到符合条件的mx[t]的最大值。
然后再考虑用子树S的信息更新mx,将两个数组合并的操作次数max[L1,L2]

可以考虑按照每棵子树deep的升序依次合并子树。从总体来看,由于总边数是n-1,则排序的复杂度nlogn,同时这样启发式合并使得合并的复杂度降为nlogn

参考代码:

#include<bits/stdc++.h>
using namespace std;
#define PI acos(-1.0)
#define pii pair<int,int>
#define mkp make_pair
#define pb push_back
#define fi first
#define se second
#define mod 1000000007
typedef long long ll;
const int INF=0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3fll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} const int maxn=2e5+;
int n,m,k,cnt,rt,sum,mx_dep,ans;
int dis[maxn],f[maxn],mx[maxn],tmp[maxn];
int head[maxn],vis[maxn],siz[maxn];
int col[maxn],deep[maxn];
vector<pii> vec; struct Edge{
int v,w,nxt;
} edge[maxn<<]; void addedge(int u,int v,int w)
{
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].nxt=head[u];
head[u]=cnt++;
} void getroot(int x,int fa)
{
siz[x]=;f[x]=;
for(int i=head[x];~i;i=edge[i].nxt)
{
int v=edge[i].v;
if(v!=fa && !vis[v])
{
getroot(v,x);
f[x]=max(f[x],siz[v]);
siz[x]+=siz[v];
}
}
f[x]=max(f[x],sum-siz[x]);
if(f[x]<f[rt]) rt=x;
} void getdis(int x,int fa)
{
mx_dep=max(mx_dep,deep[x]);
for(int i=head[x];~i;i=edge[i].nxt)
{
int v=edge[i].v;
if(!vis[v] && v!=fa)
{
deep[v]=deep[x]+col[v];
dis[v]=dis[x]+edge[i].w;
getdis(v,x);
}
}
} void getmx(int x,int fa)
{
tmp[deep[x]]=max(tmp[deep[x]],dis[x]);
for(int i=head[x];~i;i=edge[i].nxt)
{
int v=edge[i].v;
if(!vis[v] && v!=fa) getmx(v,x);
}
} void solve(int x,int S)
{
vis[x]=; vec.clear();
if(col[x]) --k;
for(int i=head[x];~i;i=edge[i].nxt)
{
if(!vis[edge[i].v])
{
mx_dep=;
deep[edge[i].v]=col[edge[i].v];
dis[edge[i].v]=edge[i].w;
getdis(edge[i].v,x);
vec.pb(mkp(mx_dep,edge[i].v));
}
} sort(vec.begin(),vec.end()); for(int i=,t=vec.size();i<t;++i)
{
getmx(vec[i].se,x);
int now=;
if(i!=)
{
for(int j=vec[i].fi;j>=;--j)
{
while(now+j<k && now<vec[i-].fi)
++now,mx[now]=max(mx[now],mx[now-]);
if(now+j<=k) ans=max(ans,tmp[j]+mx[now]);
}
}
if(i!=t-)
{
for(int j=,ct=vec[i].fi;j<=ct;++j)
mx[j]=max(mx[j],tmp[j]),tmp[j]=;
}
else
{
for(int j=,ct=vec[i].fi;j<=ct;++j)
{
if(j<=k) ans=max(ans,max(mx[j],tmp[j]));
tmp[j]=mx[j]=;
}
}
} if(col[x]) ++k; for(int i=head[x];~i;i=edge[i].nxt)
{
if(!vis[edge[i].v])
{
rt=;
sum=siz[edge[i].v];
if(siz[edge[i].v]>siz[x]) sum=S-siz[x];
getroot(edge[i].v,x);
solve(rt,sum);
}
}
} int main()
{
n=read();k=read();m=read();
int color,x,y,z;
ans=mx_dep=cnt=; sum=n; f[]=n;
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
memset(col,,sizeof(col)); for(int i=;i<=m;++i) color=read(),col[color]=;
for(int i=;i<n;++i)
{
x=read();y=read();z=read();
addedge(x,y,z);
addedge(y,x,z);
} getroot(,);
solve(rt,sum); printf("%d\n",ans); return ;
}

最新文章

  1. 前端性能优化--为什么DOM操作慢?
  2. Linux随笔之——./configure、make、make install(转)
  3. Spring知识点总结大全(1)
  4. EC笔记,第一部分:2.尽量以const,enum,inline代替#define
  5. 20145235李涛 《Java程序设计》第3周学习总结
  6. AJAX简单的数据增删改与分页应用
  7. Codeforces Round #242 (Div. 2) C题
  8. windows2003通过iis配置ftp服务器
  9. 子请求执行失败。有关更多信息,请检查 InnerException。
  10. 私人网盘系统2.0—全部升级为layUI+PHP(持续更新中)shang
  11. 用Inferno代替React开发高性能响应式WEB应用
  12. Codeforces 671 D. Roads in Yusland
  13. 【java】-- java并发包总结
  14. CF Round #551 (Div. 2) D
  15. (转)解决NSMutableAttributedString富文本,不同文字大小水平轴对齐问题(默认底部对齐)
  16. 微软Microsoft SQL server 之 MDS connection问题
  17. Python函数属性和PyCodeObject
  18. VUE2中使用mint-ui,日期选择picker
  19. 面试被问http协议?这篇文章足够覆盖所有相关问题!
  20. linux 查看cpu的使用百分比

热门文章

  1. 【持续更新】【pat】pat刷题技巧记录
  2. Hadoop压缩的图文教程
  3. nyoj 74-小学生算术(进位问题)
  4. nyoj 463-九九乘法表
  5. volatile变量能保证线程安全性吗?为什么?
  6. PostgreSQL空间数据库创建备份恢复(PostGIS vs ArcGIS)
  7. linux下制作linux系统盘(光盘、U盘)
  8. 使用class关键字创建类组件、props参数
  9. vant-ui的van-uploader上传图片
  10. 达梦&quot;记录超长&quot;警告