/*
-----------------------
[题解]
https://www.luogu.org/blog/IRving1-1/solution-sp1825
-----------------------
O(Nlog^2)做法,vjudge上写的是时限100ms,过2e5数据
-----------------------
统计tmp[i]为有i个黑点的最长路径,进行转移
合并的顺序很巧妙,也很重要,这里倒序枚举当前子树的j(tmp[j]),则可以做到控制维护之前子树cur(maxn[cur])单调递增
用maxn[i]记录小于等于i个黑点的最长路径,更新答案完了以后用当前的tmp[]更新maxn[]
记得清空tmp[]和maxn[]
-----------------------2019.2.12
*/
#pragma GCC optimize(2)
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar('\n')
#define fr friend inline
#define y1 poj
#define mp make_pair
#define pr pair<int,int>
#define fi first
#define sc second
#define pb push_back
#define lowbit(x) x & (-x)
#define B printf("Bug\n"); using namespace std;
typedef long long ll;
const int M = ;
const int N = ;
const int INF = 1e9;
const double eps = 1e-; int read()
{
int x = ,op = ;char ch = getchar();
while(ch < '' || ch > '') {if(ch == '-') op = -;ch = getchar();}
while(ch >= '' && ch <= '') x = x * + ch - '',ch = getchar();
return x * op;
} struct edge
{
int next,to,from,v;
}e[M<<]; int n,k,m,maxn[M],G,size[M],dis[M],dep[M],sum,x,y,z,head[M],ecnt,hson[M],ans,tmp[M],mdep;
bool black[M],vis[M];
vector <pr> v; void add(int x,int y,int z)
{
e[++ecnt].to = y;
e[ecnt].next = head[x];
e[ecnt].from = x;
e[ecnt].v = z;
head[x] = ecnt;
} void getG(int x,int fa)
{
size[x] = ,hson[x] = ;
for(int i = head[x];i;i = e[i].next)
{
if(e[i].to == fa || vis[e[i].to]) continue;
getG(e[i].to,x);
size[x] += size[e[i].to],hson[x] = max(hson[x],size[e[i].to]);
}
hson[x] = max(hson[x],sum - size[x]);
if(hson[x] < hson[G]) G = x;
} void getdis(int x,int fa,int d,int depth)
{
dis[x] = d,dep[x] = depth,mdep = max(mdep,dep[x]);
for(int i = head[x];i;i = e[i].next)
{
if(e[i].to == fa || vis[e[i].to]) continue;
getdis(e[i].to,x,d + e[i].v,depth + black[e[i].to]);
}
} void getmaxn(int x,int fa)
{
tmp[dep[x]] = max(tmp[dep[x]],dis[x]);
for(int i = head[x];i;i = e[i].next)
{
if(vis[e[i].to] || e[i].to == fa) continue;
getmaxn(e[i].to,x);
}
} void solve(int x)
{
vis[x] = ,v.clear();
if(black[x]) k--;
for(int i = head[x];i;i = e[i].next)
{
if(vis[e[i].to]) continue;
mdep = ,getdis(e[i].to,x,e[i].v,black[e[i].to]);//算出最长路径有几个黑点mdep
v.pb(mp(mdep,e[i].to));
}
sort(v.begin(),v.end());
rep(i,,(int)(v.size()-))
{
getmaxn(v[i].sc,x);//算出有i个黑点的最长路径长度tmp[i]
int cur = ;
if(i != )
per(j,v[i].fi,)//启发式合并:[倒序]枚举j并控制cur+j<k,这个思想很巧妙,很重要 O(N)
{
//一直没看到这个-1
while(cur + j < k && cur < v[i-].fi) cur++,maxn[cur] = max(maxn[cur],maxn[cur-]);//小于等于i个黑点的最长路径maxn[i]
if(cur + j <= k) ans = max(ans,maxn[cur] + tmp[j]);
}
if(i != (int)(v.size() - )) rep(j,,v[i].fi) maxn[j] = max(maxn[j],tmp[j]),tmp[j] = ;//小于等于i个黑点的最长路径maxn[i],并准备转移
else rep(j,,v[i].fi){if(j <= k) ans = max(ans,max(tmp[j],maxn[j]));tmp[j] = maxn[j] = ;}//最后一个子树,直接统计
}
if(black[x]) k++;
for(int i = head[x];i;i = e[i].next)
{
if(vis[e[i].to]) continue;
sum = size[e[i].to],G = ;
getG(e[i].to,x),solve(G);
}
} int main()
{
n = read(),k = read(),m = read();
rep(i,,m) x = read(),black[x] = ;
rep(i,,n-) x = read(),y = read(),z = read(),add(x,y,z),add(y,x,z);
sum = n,hson[G] = INF,getG(,);
solve(G);
printf("%d\n",ans);
return ;
}

[SP1825] Free tour II的更多相关文章

  1. SPOJ 1825 Free tour II (树的点分治)

    题目链接 Free tour II 题意:有$N$个顶点的树,节点间有权值, 节点分为黑点和白点. 找一条最长路径使得 路径上黑点数量不超过K个 这是树的点分治比较基本的题,涉及树上启发式合并……仰望 ...

  2. SP1825 【FTOUR2 - Free tour II】

    # \(SP1825\) 看到没有人用老师的办法,于是自己写一下思路 思路第一步:排除旧方法 首先这道题和\(4178\)不一样,因为那道题是计数,而这道题是求最值,最值有个坏处,就是对于来自相同子树 ...

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

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

  4. 【SPOJ】1825. Free tour II(点分治)

    http://www.spoj.com/problems/FTOUR2/ 先前看了一会题解就自己yy出来了...对拍过后交tle.................. 自己造了下大数据........t ...

  5. spoj 1825 Free tour II

    http://www.spoj.com/problems/FTOUR2/ After the success of 2nd anniversary (take a look at problem FT ...

  6. SPOJ1825 FTOUR2 - Free tour II

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

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

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

  8. SPOJ FTOUR2 - Free tour II

    Description 有些黑点,问你选择不超过 \(k\) 个黑点的路径,路径权值最大是多少. Sol 点分治. 这是qzc的论文题,不过我感觉他的翻译好强啊...我还是选择了自己去看题目... 点 ...

  9. SPOJ1825:Free tour II

    题意 luogu的翻译 给定一棵n个点的树,树上有m个黑点,求出一条路径,使得这条路径经过的黑点数小于等于k,且路径长度最大 Sol 点分治辣 如果是等于\(k\)的话,开个桶取\(max\)就好了 ...

随机推荐

  1. Spring中使用Velocity模板

    使用Velocity模板 Velocity是一种针对Java应用的易用的模板语言.Velocity模板中没有任何 Java代码,这使得它能够同时被非开发人员和开发人员轻松地理解.Velocity的用户 ...

  2. 690. Employee Importance员工权限重要性

    [抄题]: You are given a data structure of employee information, which includes the employee's unique i ...

  3. 616. Add Bold Tag in String加粗字符串

    [抄题]: Given a string s and a list of strings dict, you need to add a closed pair of bold tag <b&g ...

  4. opennebula 模板参数说明

    两种模板配置方式一.光驱引导启动需要配置:disk1:磁盘类型:cdrom      驱动类型:file      磁盘标记:hd      是否只读:yesDisk2:磁盘类型:DATABLOCK驱 ...

  5. 搭建大数据hadoop完全分布式环境遇到的坑

    搭建大数据hadoop完全分布式环境,遇到很多问题,这里记录一部分,以备以后查看. 1.在安装配置完hadoop以后,需要格式化namenode,输入指令:hadoop namenode -forma ...

  6. Linux编译提速

    一.简介 项目越来越大,重新编译整个项目是一件很费时的事,总结可以帮助提速方法,如下: 1)tmpfs: 解决IO瓶颈,充分利用本机内存资源 2)make -j: 充分利用本机计算资源 3)distc ...

  7. 第十九课 pluginlib&Nodelet

    把rgb摄像头的数据转换为laser的时候使用了Nodelet. pluginlib(插件库) 在ros中有一个plugin的包,下面是一个ROS Plugin Registration的例子 上面包 ...

  8. (一)在HTML页面中实现一个简单的Tab

    在HTML页面中实现一个简单的Tab 为了充分利用有限的HTML页面空间,经常会采用类似与TabControl的效果通过切换来显示更多的内容.本文将采用一种最为简单的方法来实现类似如Tab页切换的效果 ...

  9. APUE(1)----UNIX基础知识

    一.UNIX体系结构 所有操作系统都为他们所运行的程序提供服务,典型的服务包括:执行新程序.打开文件.读文件.分配存储区等.严格意义上来说,操作系统可以定义为一种软件,它控制计算机硬件资源,提供程序运 ...

  10. centos7 安装pip

    首先安装 python3 安装过程1.安装相关依赖 1 sudo yum install openssl-devel -y 2 sudo yum install zlib-devel -y 2.安装s ...