题目链接

一道神奇的点分治

貌似有很多做法,我觉得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的更多相关文章

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

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

  2. SPOJ1825 FTOUR2 - Free tour II

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

  3. 【SPOJ1825】Free tour II (点分治,启发式)

    题意: 边权可能为负 思路: 感觉我自己写的还是太过僵硬了,可以灵活一点,比如可以多写几个不同的dfs求出不同的信息,而不是压到同一个dfs里 #include<cstdio> #incl ...

  4. SPOJ1825:Free tour II

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

  5. SPOJ1825/FTOUR2:Free tour II——包看得懂/看不懂题解

    http://www.spoj.com/problems/FTOUR2/en/ 题目大意:给一棵黑白染色的树,求边权和最大且经过黑点不超过K的路径. ———————————————————— 前排膜拜 ...

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

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

  7. spoj 1825 Free tour II

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

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

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

  9. SPOJ FTOUR2 - Free tour II

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

随机推荐

  1. Cglib 与 JDK动态代理的运行性能比较

    都说 Cglib 创建的动态代理的运行性能比 JDK 动态代理能高出大概 10 倍,今日抱着怀疑精神验证了一下,发现情况有所不同,遂贴出实验结果,以供参考和讨论. 代码很简单,首先,定义一个 Test ...

  2. Web终端SSH功能

    http://www.laozuo.org/10703.html------ CentOS安装配置GateOne实现Web终端SSH功能

  3. Spring MVC静态资源处理:<mvc:resources />

    优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...

  4. 2018/03/23 每日一个Linux命令 之 sed

    官方定义: Linux sed命令是利用script来处理文本文件. sed可依照script的指令,来处理.编辑文本文件. Sed主要用来自动编辑一个或多个文件:简化对文件的反复操作:编写转换程序等 ...

  5. 使用QT设计师-信号和槽signal-slot(第一弹)

    自定义信号和槽的步骤: 1.定义信号---signal1 = pyqtSignal() 2.定义槽信号---def setSlot(self): 3.连接信号和槽函数---signal1.connec ...

  6. linux命令:locate

    1.命令简介         locate(locate) 命令用来查找文件或目录. locate命令要比find -name快得多,原因在于它不搜索具体目录,而是搜索一个数据库/var/lib/ml ...

  7. SqlAlchemy个人学习笔记完整汇总-转载

    使用 sqlalchemy 有3种方式: 方式1, 使用raw sql; 方式2, 使用SqlAlchemy的sql expression; 方式3, 使用ORM.   前两种方式可以统称为 core ...

  8. 深入理解Fabric环境搭建的详细过程(转)

    前面的准备工作我就不用多说了,也就是各种软件和开发环境的安装,安装好以后,我们git clone下来最新的代码,并切换到v1.0.0,并且下载好我们需要使用的docker镜像,也就是到步骤6,接下来我 ...

  9. logstash采集tomcat日志、mysql错误日志

    input{ file { path => "/opt/Tomcat7.0.28/logs/*.txt" start_position => "beginni ...

  10. PAT 1042 Shuffling Machine[难]

    1042 Shuffling Machine (20)(20 分) Shuffling is a procedure used to randomize a deck of playing cards ...