JZOJ 5257. 小X的佛光 (Standard IO)
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)的更多相关文章
- [jzoj]5257.小X的佛光
Link https://jzoj.net/senior/#main/show/5257 Problem Solution 5~90分 我们可以根据特殊性质搞 如果数据小,直接暴力在树上面模拟一次 如 ...
- JZOJ 1349. 最大公约数 (Standard IO)
1349. 最大公约数 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 小菜的妹妹小诗就要读小学了!正所谓 ...
- JZOJ 2137. 【GDKOI2004】城市统计 (Standard IO)
2137. [GDKOI2004]城市统计 (Standard IO) Time Limits: 1000 ms Memory Limits: 128000 KB Detailed Limits ...
- JZOJ 5326. LCA 的统计 (Standard IO)
5326. LCA 的统计 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Description Input Output S ...
- JZOJ 5307. 【NOIP2017提高A组模拟8.18】偷窃 (Standard IO)
5307. [NOIP2017提高A组模拟8.18]偷窃 (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Description ...
- JZOJ 5286. 【NOIP2017提高A组模拟8.16】花花的森林 (Standard IO)
5286. [NOIP2017提高A组模拟8.16]花花的森林 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Descript ...
- 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 ...
- JZOJ 5258. 友好数对 (Standard IO)
5258. 友好数对 (Standard IO) Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits Description I ...
- JZOJ 1736. 扑克游戏 (Standard IO)
1736. 扑克游戏 (Standard IO) Time Limits: 1000 ms Memory Limits: 128000 KB Description 有一棵无穷大的满二叉树,根为sta ...
随机推荐
- 05 - Tomcat 线程池的配置与优化
添加 Executor 在server.xml中的Service节点里面,增加executor节点,然后配置connector的executor属性,如下: <Executor name=&qu ...
- sql 新增随机数
update RemoteDetection set humidity=round((rand()*3+29),0),TEMPERATURE=round((rand()*3+16),0),atmosp ...
- linear correlation coefficient|Correlation and Causation|lurking variables
4.4 Linear Correlation 若由SxxSyySxy定义则为: 所以为了计算方便: 所以,可以明白的是,Sxx和Sx是不一样的! 所以,t r is independent of th ...
- 收集到的技术相关网址——python
1.Python中常用数据库访问接口模块 专用数据库连接模块——MySQL.SQLite.PostgreSQL.Oracle.IBM DB2.Infomix.Interbase.Sybase.SQL ...
- 91)PHP,cookie代码展示
cookie练习的代码: (1)先设置:setcookie('key值‘,’value值’): (2)然后我执行那个文件, (3)获取我的cookie值,用$_cookie['key值’] cook ...
- python--防止SQL注入
from pymysql import * def main(): # 创建Connextion连接 conn = connect(host='localhost', port=3306, user= ...
- SpringSecurity 如何提示错误
1.可以通过authentication-failure-url="/login.html?error=1" 前端接收参数,根据参数提示 错误 2.前端vue this.myNam ...
- less 的使用方法总结
一. 安装和使用 LESS 1.1 安装 使用命令行安装 LESS npm install -g less 1.2 使用 less 有多种的使用方法,在这里我向大家介绍最常用的俩种方法. 第一种是直接 ...
- Flask添加新命令
代码: import click from flask import Flask app = Flask(__name__) @app.cli.command() def hg(): click.ec ...
- Win10+GTX906M+Tensorflow-gpu==2.1.0
环境 Windows10 GeForce GTX 960M python 3.7.6 tensorflow-gpu==2.1.0 CUDA 10.2 cuDNN v7.9.4.38 for windo ...