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 ;
}

SPOJ Free TourII(点分治+启发式合并)的更多相关文章

  1. SPOJ:Free tour II (树分治+启发式合并)

    After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, ...

  2. SP1825 FTOUR2 - Free tour II 点分治+启发式合并+未调完

    题意翻译 给定一棵n个点的树,树上有m个黑点,求出一条路径,使得这条路径经过的黑点数小于等于k,且路径长度最大 Code: #include <bits/stdc++.h> using n ...

  3. CodeForces 958F3 Lightsabers (hard) 启发式合并/分治 多项式 FFT

    原文链接http://www.cnblogs.com/zhouzhendong/p/8835443.html 题目传送门 - CodeForces 958F3 题意 有$n$个球,球有$m$种颜色,分 ...

  4. 【题解】P4755 Beautiful Pair(启发式合并的思路+分治=启发式分治)

    [题解]P4755 Beautiful Pair upd: 之前一个first second烦了,现在AC了 由于之前是直接抄std写的,所以没有什么心得体会,今天自己写写发现 不知道为啥\(90\) ...

  5. P5979 [PA2014]Druzyny dp 分治 线段树 分类讨论 启发式合并

    LINK:Druzyny 这题研究了一下午 终于搞懂了. \(n^2\)的dp很容易得到. 考虑优化.又有大于的限制又有小于的限制这个非常难处理. 不过可以得到在限制人数上界的情况下能转移到的最远端点 ...

  6. CF932F Escape Through Leaf 斜率优化、启发式合并

    传送门 \(DP\) 设\(f_i\)表示第\(i\)个节点的答案,\(S_i\)表示\(i\)的子节点集合,那么转移方程为\(f_i = \min\limits_{j \in S_i} \{a_i ...

  7. [2016北京集训试题7]thr-[树形dp+树链剖分+启发式合并]

    Description Solution 神仙操作orz. 首先看数据范围,显然不可能是O(n2)的.(即绝对不是枚举那么简单的),我们考虑dp. 定义f(x,k)为以x为根的子树中与x距离为k的节点 ...

  8. 【20181026T2】**图【最小瓶颈路+非旋Treap+启发式合并】

    题面 [错解] 最大最小?最小生成树嘛 蛤?还要求和? 点分治? 不可做啊 写了个MST+暴力LCA,30pts,140多行 事后发现30分是给dijkstra的 woc [正解] 树上计数问题:①并 ...

  9. 【主席树启发式合并】【P3302】[SDOI2013]森林

    Description 给定一个 \(n\) 个节点的森林,有 \(Q\) 次操作,每次要么将森林中某两点联通,保证操作后还是个森林,要么查询两点间权值第 \(k\) 小,保证两点联通.强制在线. L ...

随机推荐

  1. C#同级catch块和finally块中全都抛出异常,上一级捕获哪一个?

    C#同级catch块和finally块中全都抛出异常,上一级优先捕获finally块中的异常. 测试代码: using System; namespace test { class Program { ...

  2. 【Go 入门学习】第一篇关于 Go 的博客--Go 爬虫初体验

    一.写在前面 其实早就该写这一篇博客了,为什么一直没有写呢?还不是因为忙不过来(实际上只是因为太懒了).不过好了,现在终于要开始写这一篇博客了.在看这篇博客之前,可能需要你对 Go 这门语言有些基本的 ...

  3. 201871010114-李岩松《面向对象程序设计(java)》第十二周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  4. Java设计模式(20):命令模式

    本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景 1.场景描述 智能电脑的品牌越来越多,由此诞生了一款电脑控制的APP,万能遥控器,用户在使用遥控器的时候,可以切换为自家电视的品 ...

  5. MyBatis批量插入模板

    oracle: <insert id="insertBatch" parameterType="List"> INSERT INTO TStuden ...

  6. 本地Git连接GitLab(服务器)远程仓库

    1.简介 远程仓库是指托管在网络上的项目仓库,现在互联网上有很多项目托管平台,比如github.gitlab等.为了不公开自己项目代码,可以在自己的服务器上搭建自己的项目仓库,最常见的是搭建GitLa ...

  7. nyoj 100-1的个数 (因为只统计1的个数,连栈都不需要了)

    100-1的个数 内存限制:64MB 时间限制:3000ms 特判: No 通过数:33 提交数:42 难度:1 题目描述: 小南刚学了二进制,他想知道一个数的二进制表示中有多少个1,你能帮他写一个程 ...

  8. KDevelop

    ctags+vim还是太累了,还是使用IDE好,尤其是c++模板.KDevelop就不错,符号智能推导以及cmake项目管理和配置,还是挺好用的. Android端的ndk开发使用Android St ...

  9. c# 为什么会出现死锁?多线程死锁怎么解决

    出现死锁必须满足以下几个条件: 1.互斥条件:该进程拥有的资源,其他进程只能等待其释放. 2.不剥夺条件:该进程拥有的资源只能由它自己来释放. 3.请求和保持条件:请求其他的资源,同时自己拥有的资源又 ...

  10. 如何理解Nginx, WSGI, Flask(Django)之间的关系

    如何理解Nginx, WSGI, Flask(Django)之间的关系 值得指出的是,WSGI 是一种协议,需要区分几个相近的名词: uwsgi 同 wsgi 一样也是一种协议,uWSGI服务器正是使 ...