[SP1825] Free tour II
/*
-----------------------
[题解]
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的更多相关文章
- SPOJ 1825 Free tour II (树的点分治)
题目链接 Free tour II 题意:有$N$个顶点的树,节点间有权值, 节点分为黑点和白点. 找一条最长路径使得 路径上黑点数量不超过K个 这是树的点分治比较基本的题,涉及树上启发式合并……仰望 ...
- SP1825 【FTOUR2 - Free tour II】
# \(SP1825\) 看到没有人用老师的办法,于是自己写一下思路 思路第一步:排除旧方法 首先这道题和\(4178\)不一样,因为那道题是计数,而这道题是求最值,最值有个坏处,就是对于来自相同子树 ...
- SP1825 FTOUR2 - Free tour II 点分治+启发式合并+未调完
题意翻译 给定一棵n个点的树,树上有m个黑点,求出一条路径,使得这条路径经过的黑点数小于等于k,且路径长度最大 Code: #include <bits/stdc++.h> using n ...
- 【SPOJ】1825. Free tour II(点分治)
http://www.spoj.com/problems/FTOUR2/ 先前看了一会题解就自己yy出来了...对拍过后交tle.................. 自己造了下大数据........t ...
- spoj 1825 Free tour II
http://www.spoj.com/problems/FTOUR2/ After the success of 2nd anniversary (take a look at problem FT ...
- SPOJ1825 FTOUR2 - Free tour II
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- SPOJ:Free tour II (树分治+启发式合并)
After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, ...
- SPOJ FTOUR2 - Free tour II
Description 有些黑点,问你选择不超过 \(k\) 个黑点的路径,路径权值最大是多少. Sol 点分治. 这是qzc的论文题,不过我感觉他的翻译好强啊...我还是选择了自己去看题目... 点 ...
- SPOJ1825:Free tour II
题意 luogu的翻译 给定一棵n个点的树,树上有m个黑点,求出一条路径,使得这条路径经过的黑点数小于等于k,且路径长度最大 Sol 点分治辣 如果是等于\(k\)的话,开个桶取\(max\)就好了 ...
随机推荐
- 【总结整理】OpenLayers项目分析,OpenLayers中的图层,GeoServer发布wms服务--实验(转)
https://blog.csdn.net/u013751758/article/details/44751315 https://blog.csdn.net/u013751758/article/d ...
- PHP防止木马攻击的措施
防止跳出web目录 只允许你的PHP脚本在web目录里操作,针对Apache,还可以修改httpd.conf文件限制PHP操作路径. 例如:php_admin_value open_basedir( ...
- Django----Rest Framework框架
Django Rest Framework框架(10) - RESTful规范 1.API与用户的通信协议,总是使用HTTPs协议. 2.域名 https://api.example.com 尽量将A ...
- ByteUnit
JDK里面有TimeUnit,看spark源码有个ByteUnit.这个类还是挺不错的. public enum ByteUnit { BYTE (1), KiB (1024L), MiB ((lon ...
- App测试从入门到精通之App分类和场景操作系统
App概要 APP是application的缩写.通常指的是手机软件上的应用,或称为手机客户端.手机app就是手机的应用程序.随着智能手机的越发普及,用户越发依赖手机软件商品店,app开发的需求与发展 ...
- 三年经验的C,超过两题答不出请离开软件界
1.double free是什么问题?申请地址与释放地址不一致会有什么问题? 2.main函数最多有几个参数?各是什么作用? 3.crt是什么?编译器是怎么样连接crt的(描述cl或者gcc方式) 4 ...
- div高度自适应窗口高度布局
给body和html都设置height:100%:然后子元素用百分比设置高度
- 将“100px” 转换为100
parseInt("100px") //结果是100
- Sql 不确定列 行转列操作
做项目时,用到了汇总统计的行转列,且 表结构: 具体存储过程脚本如下: -- =============================================-- Author: -- C ...
- 从头开始学eShopOnContainers——设置WebSPA单页应用程序
一.简介 Web SPA单页应用程序需要一些额外的步骤才能使其工作,因为它需要在生成Docker镜像之前构建JavaScript框架依赖项和JS代码. 二.安装基础环境 1.安装NPM 为了能够使用n ...