5257. 小X的佛光 (Standard IO)

Time Limits: 2000 ms Memory Limits: 524288 KB

Description

Input

Output

Sample Input

3 3 1

1 2

2 3

1 2 3

1 1 3

3 1 3

Sample Output

1

1

3

Data Constraint

Hint

样例2、3、4见所附文件

题解

n个城市n−1条边,很明显是一颗树

画多几个图,就会发现可以用lca分类讨论

对于三个点x,z,y,要求的是x−>z和y−>z重合部分的长度,即点的个数

有三种情况

设三点lca分别是fxy,fyz,fxz

第一种是fxz=fyz,这样就是z的深度+/−fxz的深度+1

第二种是fxy=fyz,这样就是z的深度−fxz的深度+1

第三种是fxz=fxy,这样就是z的深度−fyz的深度+1

求lca用tarjan会爆栈,虽然我之前写过手打栈版,但是由于太麻烦了,就用了倍增

代码

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define N 200010
using namespace std; vector<long>map[N];
queue<long>que;
bool b[N];
long dep[N],fa[N][20]; void build(long x)
{ long now,to,i;
que.push(x);
while(!que.empty()){
now=que.front();
que.pop();
b[now]=true;
for(i=0;i<map[now].size();i++){
to=map[now][i];
if(!b[to]){
fa[to][0]=now;
dep[to]=dep[now]+1;
que.push(to);
}
}
}
} long n; void init()
{ long i,j;
for(j=1;(1<<j)<=n;j++)
for(i=1;i<=n;i++)
fa[i][j]=fa[fa[i][j-1]][j-1];
} long lca(long x,long y)
{ long up;
if(dep[x]>dep[y])
swap(x,y);
up=19;
while(dep[y]>dep[x]){
while(dep[y]-(1<<up)<dep[x]&&up>=0)
up--;
if(up<0)break;
y=fa[y][up--];
}
up=19;
while(x!=y){
while(fa[x][up]==fa[y][up]&&up>=0)
up--;
if(up<0)break;
x=fa[x][up];
y=fa[y][up];
up--;
}
if(x==y)
return x;
else
return fa[x][0];
} int main()
{ long m,q,x,y,z,fxy,fxz,fyz,i,ans;
scanf("%ld%ld%ld",&n,&m,&q);
for(i=1;i<n;i++){
scanf("%ld%ld",&x,&y);
map[x].push_back(y);
map[y].push_back(x);
}
build(1);
init();
for(i=1;i<=m;i++){
scanf("%ld%ld%ld",&x,&z,&y);
fxy=lca(x,y);
fxz=lca(x,z);
fyz=lca(y,z);
if(fxz==fyz&&fxz!=fxy)
if(fxz==z)
ans=dep[fxy]-dep[z]+1;
else
ans=dep[fxy]+dep[z]-dep[fxz]*2+1;
else if(fxy==fyz&&fxz!=fxy)
ans=dep[z]-dep[fxz]+1;
else
ans=dep[z]-dep[fyz]+1;
printf("%ld\n",ans);
}
return 0;
}

JZOJ 5257. 小X的佛光 (Standard IO)的更多相关文章

  1. [jzoj]5257.小X的佛光

    Link https://jzoj.net/senior/#main/show/5257 Problem Solution 5~90分 我们可以根据特殊性质搞 如果数据小,直接暴力在树上面模拟一次 如 ...

  2. JZOJ 1349. 最大公约数 (Standard IO)

    1349. 最大公约数 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 小菜的妹妹小诗就要读小学了!正所谓 ...

  3. JZOJ 2137. 【GDKOI2004】城市统计 (Standard IO)

    2137. [GDKOI2004]城市统计 (Standard IO) Time Limits: 1000 ms  Memory Limits: 128000 KB  Detailed Limits  ...

  4. JZOJ 5326. LCA 的统计 (Standard IO)

    5326. LCA 的统计 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Description Input Output S ...

  5. JZOJ 5307. 【NOIP2017提高A组模拟8.18】偷窃 (Standard IO)

    5307. [NOIP2017提高A组模拟8.18]偷窃 (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Description ...

  6. JZOJ 5286. 【NOIP2017提高A组模拟8.16】花花的森林 (Standard IO)

    5286. [NOIP2017提高A组模拟8.16]花花的森林 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Descript ...

  7. JZOJ 5305. 【NOIP2017提高A组模拟8.18】C (Standard IO)

    5305. [NOIP2017提高A组模拟8.18]C (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Description ...

  8. JZOJ 5258. 友好数对 (Standard IO)

    5258. 友好数对 (Standard IO) Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits Description I ...

  9. JZOJ 1736. 扑克游戏 (Standard IO)

    1736. 扑克游戏 (Standard IO) Time Limits: 1000 ms Memory Limits: 128000 KB Description 有一棵无穷大的满二叉树,根为sta ...

随机推荐

  1. SG函数学习

    尼姆博弈就是sg函数的简单体现 学习粗:https://blog.csdn.net/luomingjun12315/article/details/45555495 //f[N]:可改变当前状态的方式 ...

  2. Excel-DNA项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制【VB.Net版】

    Excel-DNA项目中的自定义功能区和自定义任务窗格需要用到各种命名空间.添加所需文件,才能实现.后来我发现可以把所有代码都写在Class1.vb这个默认文件中. 大家可以在Visual Studi ...

  3. TreeviewEditor.rar

    本工具可以打开.保存指定格式的XML文件. 树形控件的节点可以编辑.删除.增加.使用本工具看方便地创建书或论文的目录大纲,我用这个工具已经写了好几本书了. 动态图1: 动态图2:编辑效果,支持节点拖曳 ...

  4. centos jdk

    yum list java* yum install xxx -y java -version /* 可省略 */ vi /etc/profile export JAVA_HOME=/usr/lib/ ...

  5. The Tower(ccpc吉林)

    http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1005&cid=867 #include<iostream> ...

  6. 源码中TODO、FIXME和XXX的含义

    前言: 今天在阅读Qt  Creator的源代码时,发现一些注释中有FIXME英文单词,用英文词典居然查不到其意义! 实际上,在阅读一些开源代码时,我们常会碰到诸如:TODO.FIXME和XXX的单词 ...

  7. 深入JVM内核--GC算法和种类

    GC的概念 Garbage Collection 垃圾收集 1960年 List 使用了GC Java中,GC的对象是堆空间和永久区 引用计数法 老牌垃圾回收算法 通过引用计算来回收垃圾 使用者 CO ...

  8. Disk Group基础概念与深度解析

  9. shell-变量学习-01

    1.变量   1.1 变量赋值 > variable_zhou="hello world!" #等号两边不能有空格   1.2 使用变量 > echo $variabl ...

  10. fiddler模拟返回响应数据

    通过find查找修改指定内容 选择find a file 保存后重新发起请求