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的论文题,不过我感觉他的翻译好强啊...我还是选择了自己去看题目... 点 ...
随机推荐
- Android开发小技巧之根据position判断ListView是否在显示
使用ListView的时候,会有判断指定项是否正在显示的需求,其实很简单,代码如下: private boolean isShowing(int position) { int showViewCou ...
- table中强制不换行
总是一些文章说要强制换行,很少提到说如何不换行. 一般都会使用word-break: keep-all;使得强制不换行. HTML <!DOCTYPE html PUBLIC "-// ...
- HDU 2187 - 悼念512汶川大地震遇难同胞——老人是真饿了 - [大水题]
讲真,这么水的题,我都不怎么好意思扔到博客上来,但是没办法啊,我总得证明一下今天上午我不是在寝室里瞎玩浪费掉的…… 思路就是,把米按单价从小到大排个序,便宜的买的越多越好,直到钱花光为止……我真的都不 ...
- Mergeable Stack 直接list内置函数。(152 - The 18th Zhejiang University Programming Contest Sponsored by TuSimple)
题意:模拟栈,正常pop,push,多一个merge A B 形象地说就是就是将栈B堆到栈A上. 题解:直接用list 的pop_back,push_back,splice 模拟, 坑:用splice ...
- CodeForces - 665D Simple Subset 想法题
//题意:给你n个数(可能有重复),问你最多可以取出多少个数使得任意两个数之和为质数.//题解:以为是个C(2,n)复杂度,结果手摸几组,发现从奇偶性考虑,只有两种情况:有1,可以取出所有的1,并可以 ...
- postgresql+postgis+pgrouting安装步骤图解
1.在此(https://www.bigsql.org/postgresql/installers.jsp/)下载postgresql(开源数据库,gis行业推荐使用); 2.在此(http://wi ...
- 网站编辑须知9个SEO技巧
1. 文章当中最好需要出现一个网站核心关键词所谓的网站核心的关键词就是指与网站内容相关性比较高并且是比较受欢迎的关键词,当然还有相当高的转化率 2. 文章标题当中需要出现关键词.关键字在标题标签< ...
- java string.getBytes(“UTF-8”) javascript equivalent
1. byte[] bytes = "test.message".getBytes("UTF-8"); //result: [116, 101, 115, 11 ...
- SQL SERVER 聚集索引 非聚集索引 区别
转自http://blog.csdn.net/single_wolf_wolf/article/details/52915862 一.理解索引的结构 索引在数据库中的作用类似于目录在书籍中的作用,用来 ...
- mysql 权限管理 对所有库 所有表 授权 *.*
对miek这个账号localhost 授予了所有库,所表的select权限 mysql> grant select on *.* to 'mike'@'localhost'; Query OK, ...