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的论文题,不过我感觉他的翻译好强啊...我还是选择了自己去看题目... 点 ...
随机推荐
- Python开发环境搭建方法简述
先插入一条广告,博主新开了一家淘宝店,经营自己纯手工做的发饰,新店开业,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!店名: 小鱼尼莫手工饰品店经营: 发饰.头花.发夹.耳环等(手工制作)网店: ...
- Git之远程仓库
1,注册账号 登录https://github.com注册一个账号 2,上传公钥 本地CentOS使用命令 ssh-keygen -t rsa生成秘钥 复制秘钥输入到github网站 3,新建仓库 4 ...
- IIS 下载文件 报错“401 - 未授权: 由于凭据无效,访问被拒绝。”
点开身份验证 改为启用就OK了 重启一下IIS. 如果你上在办法没有解决可参考 1.打开“IIS信息服务管理器”——>选择你发布的网站——>选择功能视图中的“身份验证”——>右键匿名 ...
- NEFU 118 - n!后面有多少个0 & NEFU 119 - 组合素数 - [n!的素因子分解]
首先给出一个性质: n!的素因子分解中的素数p的幂为:[ n / p ] + [ n / p² ] + [ n / p³ ] + …… 举例证明: 例如我们有10!,我们要求它的素因子分解中2的幂: ...
- Zookeeper简介及单机、集群模式搭建
1.zookeeper简介 一个开源的分布式的,为分布式应用提供协调服务的apache项目. 提供一个简单的原语集合,以便于分布式应用可以在它之上构建更高层次的同步服务. 设计非常易于编程,它使用的是 ...
- Oracle体系结构之Oracle基本数据字典:v$database、v$instance、v$version、dba_objects
v$database: 视图结构: SQL> desc v$database; Name Null? Type - ...
- ddt ddl dml
data-definition language 数据定义语言 data-manipulation language 数据操纵语言 data type definition 文档类型定义
- 图论——最小生成树_prim
今天是最小生成树的prim的算法,因为本人水平有限所以堆优化都不是很会啊,但邻接表好像出了点小差错所以上邻接矩阵比较好一点,尽管比Kruskal慢了很多很多但这种贪心思想还是要学习的.从第一条边开始取 ...
- 2018/04/17 每日一个Linux命令 之 tar
10天没有更新这个每日学习 linux 了,因为实在很忙,晚上还要看会其他知识. 但是也不应该给自己找理由,还是应该每天的坚持下去 -- tar 用于在 linux 解压缩/文件 这个命令下面的参数非 ...
- Github常用命令【转】
本地仓库(local repository) 创建一个本地仓库的流程: 为本地仓库创建一个目录 在目录中执行 git init 对本地仓库所做的改变(例如添加.删除文件等)首先加入到本地仓库的 Ind ...