[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\)就好了 ...
随机推荐
- 值得一做》关于并查集的进化题目 BZOJ1015(BZOJ第一页计划)(normal-)
这道题和以前做过的一道经典的洪水冲桥问题很像,主要做法是逆向思维.(BZOJ第10道非SB题纪念) 先给出题目 Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者 ...
- QPS、PV和需要部署机器数量计算公式
QPS:Queries Per Second意思是“每秒查询率”,是一台服务器每秒能够相应的查询次数,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准. TPS是 Transactions ...
- 张超超OC基础回顾01_类的创建,申明属性,以及本质
一. 类的声明和实现&规则 1.如何编写类的声明 以@interface开头 , 以@end结尾, 然后再class name对应的地方写上 事物名称, 也就是类名即可 注意: 类名的首字符必 ...
- opennebula 安装指定参数
[root@opennebula opennebula-]# ./install.sh -u oneadmin -g oneadmin -k -d /home/oneadmin/ -u 指定用户-g ...
- NOIP2018 解题笔记
D1T1 铺设道路 在场上并没有想到积木大赛这道原题. 差分之后可以把在$[l, r]$这段区间$ - 1$变成在$l$处$ - 1$,在$r + 1$处$ + 1$,然后最终目标是使$\forall ...
- 验证码测试-demo
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...
- css总结7:盒子模型理解
1 盒子模型 1.1盒子模型的盒子: 以博客园页面为例: 1.2盒子内部构造:边框(border).内容(content).填充(padding).边界(margin)---CSS盒子模式都具 ...
- DB2 函数快速构造测试数据
函数快速构造测试数据 [案例]使用DB2内置函数快速构造测试数据 无论您是在用原型证明某一概念,还是开发一个全新的应用程序,或者只是学习 SQL,您都需要在您的应用程序上运行测试数据.为了有效地测试应 ...
- C#判断程序调用外部的exe已结束
来源: C#如何判断程序调用的exe已结束 方法一:这种方法会阻塞当前进程,直到运行的外部程序退出 System.Diagnostics.Process exep = System.Diagnosti ...
- C# 操作 MongoDB
今项目使用Mongodb,C#操作MongoDB使用MongoDB.Driver.dll库(Nuget),写了个小Demo,如下: using System; using System.Collect ...