51nod 1389 跳跳树 倍增
来源:互联网 发布:制作录音广告软件 编辑:程序博客网 时间:2024/05/02 04:29
题意
小a与小b在树上玩一个游戏,每一轮游戏小a与小b在树上选两个点,然后小a朝着小b所在的方向移动。神奇的是,这棵树上每条边都有一个长度的,每个点上都有一棵非常高的松树。小a可以从高处不耗费任何体力跳往相邻的低处,也可以耗费一点体力,走不超过k的长度的路径走到某个点然后跳到松树顶端(不能停在路中央)。现在他们进行了Q次游戏,每一次小a所在的位置为xi的顶端,小b所在的位置为yi的顶端。小a想知道最少耗费多少体力能到达小b所在的位置。
1<=n,Q<=300000
分析
调死我了。。。
把询问沿着lca拆成两条链分别处理。
预处理bz[i,j]表示i花费2^j往上跳最多能跳到哪里。
然后先从x往上跳,跳到一个位置满足再跳一步就能到达或跨过lca,然后再讨论一下各种情况,再从y开始往上跳即可。
细节超多。。。
代码
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const int N=300005;int n,k,cnt,last[N],top1[N],top2[N],bz1[N][20],bz2[N][20],fa[N][20],dep[N],h[N],bin[20],stack[20];LL dis[N];struct edge{int to,next,w;}e[N*2];int read(){ int x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}void pri(int x){ if (!x) {puts("0");return;} int top=0; while (x) stack[++top]=x%10,x/=10; while (top) putchar(stack[top]+'0'),top--; puts("");}void addedge(int u,int v,int w){ e[++cnt].to=v;e[cnt].w=w;e[cnt].next=last[u];last[u]=cnt; e[++cnt].to=u;e[cnt].w=w;e[cnt].next=last[v];last[v]=cnt;}void dfs(int x){ dep[x]=dep[fa[x][0]]+1; for (int i=1;i<=18;i++) fa[x][i]=fa[fa[x][i-1]][i-1]; top1[x]=(fa[x][0]&&h[fa[x][0]]<h[x])?top1[fa[x][0]]:x; top2[x]=(fa[x][0]&&h[fa[x][0]]>h[x])?top2[fa[x][0]]:x; if (top1[x]!=x) bz1[x][0]=bz1[top1[x]][0]; else { int y=x; for (int i=18;i>=0;i--) if (dis[x]-dis[fa[y][i]]<=k) y=fa[y][i]; bz1[x][0]=top1[y]; } if (top2[x]!=x) bz2[x][0]=bz2[top2[x]][0]; else { int y=x; for (int i=18;i>=0;i--) if (dis[x]-dis[fa[y][i]]<=k) y=fa[y][i]; bz2[x][0]=top2[y]; } for (int i=1;i<=18;i++) { bz1[x][i]=bz1[bz1[x][i-1]][i-1]; bz2[x][i]=bz2[bz2[x][i-1]][i-1]; } for (int i=last[x];i;i=e[i].next) { if (e[i].to==fa[x][0]) continue; fa[e[i].to][0]=x; dis[e[i].to]=dis[x]+e[i].w; dfs(e[i].to); }}int get_lca(int x,int y){ if (dep[x]<dep[y]) swap(x,y); for (int i=18;i>=0;i--) if (dep[fa[x][i]]>=dep[y]) x=fa[x][i]; if (x==y) return x; for (int i=18;i>=0;i--) if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i]; return fa[x][0];}int main(){ bin[0]=1; for (int i=1;i<=18;i++) bin[i]=bin[i-1]*2; n=read();k=read(); for (int i=1;i<n;i++) { int x=read(),y=read(),z=read(); addedge(x,y,z); } for (int i=1;i<=n;i++) h[i]=read(); dfs(1); int q=read(); while (q--) { int x=read(),y=read(),lca=get_lca(x,y); x=(dep[top1[x]]>dep[lca])?top1[x]:lca;y=(dep[top2[y]]>dep[lca])?top2[y]:lca; if (lca==x) { int now=y,ans=0; for (int i=18;i>=0;i--) if (dep[bz2[now][i]]>dep[x]) ans+=bin[i],now=bz2[now][i]; if (top2[x]==top2[y]) ans--; pri(ans+1); } else if (lca==y) { int now=x,ans=0; for (int i=18;i>=0;i--) if (dep[bz1[now][i]]>dep[y]) ans+=bin[i],now=bz1[now][i]; if (top1[x]==top1[y]) ans--; pri(ans+1); } else { int now=x,ans=0; for (int i=18;i>=0;i--) if (dep[bz1[now][i]]>dep[lca]) ans+=bin[i],now=bz1[now][i]; int ret=0,sta=y; if (dis[now]-dis[lca]<=k) ret=k-dis[now]+dis[lca]; else sta=lca; for (int i=18;i>=0;i--) if (dis[fa[sta][i]]-dis[lca]>ret) sta=fa[sta][i]; if (sta!=lca) sta=fa[sta][0]; int ypx=y; for (int i=18;i>=0;i--) if (dep[bz2[ypx][i]]>dep[sta]) ans+=bin[i],ypx=bz2[ypx][i]; if (dis[y]-dis[lca]<=ret) ans--; pri(ans+2); } } return 0;}
阅读全文
0 0
- 51nod 1389 跳跳树 倍增
- 51nod 1628 非波那契树 (倍增)
- bzoj 2144: 跳跳棋 lca+倍增
- 【BZOJ2144】跳跳棋 模拟gcd以及倍增LCA
- 51nod 1647 小Z的trie 广义后缀自动机+倍增
- 51Nod-1353-树
- 树 (罗雨屏)倍增+dfs
- 3306: 树 树上倍增
- 跳跳跳!!
- 倍增
- 倍增
- 倍增
- 倍增
- 51nod 1631 线段树
- 51nod 1364 线段树
- 51nod 1672 线段树
- 51nod 1588 幸运树
- 51nod-1424 零树
- UIDocumentInteractionController
- HDOJ 1391 Number Steps
- C语言之旅(1)指针
- openmpi 2.1.1 多网卡运行实例
- iOS编程基础-OC(三)-对象和消息传递
- 51nod 1389 跳跳树 倍增
- 程序运行状态时,代理的回调:
- Python读写文件
- Invalid bound statement (not found): com.bookestore.dao.OrderInfoDao.findList的根本原因及解决方案
- lesson5-1
- dos下遍历目录和文件的代码(主要利用for命令)
- windows32bit安装Redis
- 教你cad版本怎么用转换器转换操作
- 1064. 朋友数(20)——C语言