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. OpenCV 图像清晰度(相机自动对焦)

    相机的自动对焦要求相机根据拍摄环境和场景的变化,通过相机内部的微型驱动马达,自动调节相机镜头和CCD之间的距离,保证像平面正好投影到CCD的成像表面上.这时候物体的成像比较清晰,图像细节信息丰富. 相 ...

  2. TreeviewEditor.rar

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

  3. MySQL数据库简单操作

    title date tags layout MySQL简单操作 2018-07-16 Linux post 登录mysql mysql -h 主机名 -u 用户名 -p 查看所有数据库 show d ...

  4. 1)public,provite和protect不能放在函数函数头

    今天我才知道,原来这三个修饰的东西,只是用在类里面方法,怪不得一个叫方法,一个叫函数了,原来就是区分他们,哎, 今天遇到这么一个问题: <?php //header('Content-type: ...

  5. 十、RPC(远程过程调用)

    相关概念 RPC,是Remote Procedure Call的简称,即远程过程调用.它是一种通过网络从远程计算机上请求服务,而不需要了解底层网络的技术.RPC的主要功用是让构建分布式计算更容易,在提 ...

  6. 题解 HDU 3698 Let the light guide us Dp + 线段树优化

    http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...

  7. labview的bool(布尔)按键机械属性

    在学习LabVIEW(简称LV)时,布尔控件是常用的控件.布尔控件分为按钮型控件和开关型控件,LV内部并没有区分按钮型还是开关型.这两种布尔控件可以根据需要相互转换,通过配置布尔控件的机械动作属性来实 ...

  8. 最长递增子序列-dp问题

    Longest Increasing Subsequence The longest increasing subsequence problem is to find a subsequence o ...

  9. Luogu_2061_[USACO07OPEN]城市的地平线City Horizon

    题目描述 Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the cit ...

  10. vue watch和computed的使用场景

    watch 监听某个数据的变化(监听完调用什么函数) 一个数据影响多个数据 (比如:浏览器自适应.监控路由对象.监控自身属性变化) computed 计算后返回新 一个数据受多个数据影响(比如:计算总 ...