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 ...
随机推荐
- C. 小花梨判连通
https://acm.ecnu.edu.cn/contest/173/problem/C/ 联通块染色,若i,j满足题目中的条件,那么他们在每幅图中的染色情况相同,即hash值相同 使用unsign ...
- ubuntu Python 升级
安装时ubuntu提示: DEPRECATION: Python 3.4 support has been deprecated. pip 19.1 will be the last one supp ...
- mac vmware fusion10 nat 模式网络配置
mac vmware fusion10 nat 模式网络配置 1.虚拟机选择 nat 模式 虚拟机-->网络适配器-->网络适配器设置-->连接网络适配器(对勾)-->与我的 ...
- android采用MVP漫画APP、适配刘海屏、小黄车主界面、录音波浪动画、综合APP等源码
Android精选源码 一款采用MVP架构的仿完整漫画APP源码 Android适配刘海屏幕 基于Xmpp协议的即时通讯社交软件(客户端+服务端) Android小黄车(ofo)app主页菜单效果 一 ...
- Offer垂青于有准备的人——微软亚洲研究院实习生们的就业分享
编者按:一年一度的"求职大战"又拉开了序幕,如何在求职中掌握主动,更好地展现自己,最后抓住Offer?且听微软亚洲研究院三位实习生慢慢道来,Offer总会垂青于有准备的人. 廖振, ...
- java中的锁——列队同步器
队列同步器 队列同步器(AbstractQueuedSynchronizer)为实现依赖于先进先出 (FIFO) 等待队列的阻塞锁和相关同步器(信号量.事件,等等)提供一个框架.此类的设计目标是成为依 ...
- DB2数据库多行一列转换成 一行一列
在db2中遇到多行一列转为一行一列的需求时,用db2函数 LISTAGG可以实现该功能.语法如下: SELECT [分组的字段 ] , LISTAGG([需要聚合的字段名], ',') FRO ...
- practically|exclamation|defy|thrilled|
ADV 简直:几乎Practically means almost, but not completely or exactly. He'd known the old man practically ...
- python循环删除list中的元素
直接上例子: a = [1,2,3,4,5,6] for i in a: a.remove(i) print(a) 返回:[2, 4, 6] 循环a,想删除a的所有元素,但实际确有数据保留了下来,这是 ...
- leetcode第23题:合并K个排序链表
首先我想到的是采用一般递归法,将K个链表合并化为(k-1)两个链表合并 class Solution: def mergeKLists(self, lists: List[ListNode]) -&g ...