spoj1825 Free tour II
一道神奇的点分治
貌似有很多做法,我觉得BIT要好些一些(雾
要求经过黑点数<k就用BIT区间查询前缀
对于每个点用 BIT[0,k-经过黑点数]的最大值+路径长度
使用点分治做到O(n*log22n)
貌似还有O(nlog2n)的做法(雾
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define rre(i,r,l) for(int i=(r);i>=(l);i--)
#define re(i,l,r) for(int i=(l);i<=(r);i++)
#define Clear(a,b) memset(a,b,sizeof(a))
#define inout(x) printf("%d",(x))
#define douin(x) scanf("%lf",&x)
#define strin(x) scanf("%s",(x))
#define LLin(x) scanf("%lld",&x)
#define op operator
#define CSC main
typedef unsigned long long ULL;
typedef const int cint;
typedef long long LL;
using namespace std;
cint inf=;
void inin(int &ret)
{
ret=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=;ch=getchar();}
while(ch>=''&&ch<='')ret*=,ret+=ch-'',ch=getchar();
ret=f?-ret:ret;
}
int head[],next[],zhi[],w[],si[],v[],ed;
int shu[],dis[],col[],bo[];
int cc[],t[];
int n,m,k,sum,ans,root,T;
void add(int a,int b,int c)
{
next[++ed]=head[a],head[a]=ed,zhi[ed]=b,v[ed]=c;
next[++ed]=head[b],head[b]=ed,zhi[ed]=a,v[ed]=c;
}
void getroot(int x,int fa)
{
w[x]=,si[x]=;
for(int i=head[x];i;i=next[i])if(!bo[zhi[i]]&&zhi[i]!=fa)
{
getroot(zhi[i],x);
si[x]+=si[zhi[i]];
w[x]=max(w[x],si[zhi[i]]);
}
w[x]=max(w[x],sum-si[x]);
if(w[x]<w[root])root=x;
}
int lowbit(int x){return x&-x;}
int query(int r)
{
int ret=-inf;r++;
while(r)
{
if(t[r]==T)ret=max(ret,cc[r]);
r-=lowbit(r);
}
return ret;
}
void add_(int c,int x)
{
c++;
while(c<=k+)
{
if(t[c]==T)cc[c]=max(cc[c],x);
else t[c]=T,cc[c]=x;
c+=lowbit(c);
}
}
void getans(int x,int fa)
{
if(shu[x]-col[root]>k)return ;
int hh=query(k-shu[x]+col[root]);
if(hh!=-inf)ans=max(ans,hh+dis[x]);
for(int i=head[x];i;i=next[i])if(!bo[zhi[i]]&&zhi[i]!=fa)
dis[zhi[i]]=dis[x]+v[i],shu[zhi[i]]=col[zhi[i]]+shu[x],getans(zhi[i],x);
}
void add(int x,int fa)
{
if(shu[x]-col[root]>k)return ;
add_(shu[x],dis[x]);
for(int i=head[x];i;i=next[i])if(!bo[zhi[i]]&&zhi[i]!=fa)
add(zhi[i],x);
}
void solve(int x)
{
T++;
bo[x]=;add_(col[x],);
for(int i=head[x];i;i=next[i])if(!bo[zhi[i]])
{
shu[zhi[i]]=col[x]+col[zhi[i]],dis[zhi[i]]=v[i];
getans(zhi[i],x);
add(zhi[i],x);
}
for(int i=head[x];i;i=next[i])if(!bo[zhi[i]])
root=,sum=si[zhi[i]],getroot(zhi[i],),solve(root);
}
int CSC()
{
inin(n),inin(k),inin(m);
re(i,,m)
{
int x;inin(x);
col[x]=;
}
re(i,,n)
{
int q,w,e;
inin(q),inin(w),inin(e);
add(q,w,e);
}
w[]=sum=n;
getroot(,);
re(i,,k+)cc[i]=-inf;
ans=-inf;
solve(root);
printf("%d",max(ans,));
return ;
}
spoj1825 Free tour II的更多相关文章
- SPOJ 1825 Free tour II (树的点分治)
题目链接 Free tour II 题意:有$N$个顶点的树,节点间有权值, 节点分为黑点和白点. 找一条最长路径使得 路径上黑点数量不超过K个 这是树的点分治比较基本的题,涉及树上启发式合并……仰望 ...
- SPOJ1825 FTOUR2 - Free tour II
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 【SPOJ1825】Free tour II (点分治,启发式)
题意: 边权可能为负 思路: 感觉我自己写的还是太过僵硬了,可以灵活一点,比如可以多写几个不同的dfs求出不同的信息,而不是压到同一个dfs里 #include<cstdio> #incl ...
- SPOJ1825:Free tour II
题意 luogu的翻译 给定一棵n个点的树,树上有m个黑点,求出一条路径,使得这条路径经过的黑点数小于等于k,且路径长度最大 Sol 点分治辣 如果是等于\(k\)的话,开个桶取\(max\)就好了 ...
- SPOJ1825/FTOUR2:Free tour II——包看得懂/看不懂题解
http://www.spoj.com/problems/FTOUR2/en/ 题目大意:给一棵黑白染色的树,求边权和最大且经过黑点不超过K的路径. ———————————————————— 前排膜拜 ...
- 【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 ...
- 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的论文题,不过我感觉他的翻译好强啊...我还是选择了自己去看题目... 点 ...
随机推荐
- java基础/一个类A继承了类B,那么A就叫做B的派生类或子类,B就叫基类或超类。
类重复,pulic class demo1 和class demo1 重复 无主类, 在cmd中输入命令: SET CLASSPATH=. (等号后为英文点符号),即可设置解释的路径为当前路径. 再次 ...
- ubuntu16.04下安装ros-kinetic
参考:http://wiki.ros.org/kinetic/Installation/Ubuntu 1.添加ROS软件源 ~$ sudo sh -c 'echo "deb http://p ...
- opencv学习网站
强烈推荐一个老外的网站,pyimagesearch 网址:https://www.pyimagesearch.com/
- oracle中动态SQL使用详细介绍
Oracle编译PL/SQL程序块分为两个种:通常静态SQL采用前一种编译方式,而动态SQL采用后一种编译方式,需要了解的朋友可以参考下 1.静态SQLSQL与动态SQL Oracle编译PL ...
- CodeForces - 55C Pie or die 想法题(猜程序)
http://codeforces.com/problemset/problem/55/C 题意:一个博弈. 题解:瞎猜,目前不清楚原理 #include<iostream> #inclu ...
- linux环境下python的部署
linux系统环境自带python2.6,但有时我们项目使用的版本可能是3.x以上等等,此时我们需要在linux中再安装项目所需的python版本,此时就涉及多版本共存问题了,很多同学在安装多个版本P ...
- paas平台
paas平台 定义:PaaS是云计算中重要的一类服务,为用户提供应用的全生命周期管理和相关的资源服务.通过PaaS,用户可以完成应用的构建.部署.运维管理,而不需要自己去搭建计算环境,如安装服务器.操 ...
- 洛谷P4168 蒲公英 [Violet] 分块
题解:分块+离散化 解题报告: 一个分块典型题呢qwq还是挺妙的毕竟是道黑题 然,然后发现忘记放链接了先放链接QAQ 有两三种解法,都港下qwq 第一个是O(n5/3)的复杂度,谢总说不够优秀没有港, ...
- qt——常用的布局方法
布局相关对象及简介 窗体上的所有的控件必须有一个合适的尺寸和位置.Qt提供了一些类负责排列窗体上的控件,主要有:QHBoxLayout,QVBoxLayout,QGridLayout,QStackLa ...
- mysql 内置功能 存储过程 目录
mysql 内置功能 存储过程介绍 mysql 内置功能 存储过程 创建无参存储过程 mysql 内置功能 存储过程 创建有参存储过程 mysql 内置功能 存储过程 删除存储过程