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 ... 
随机推荐
- wait操作接口
			1.等待回收的两个函数wait()和waitpid()函数 1.1 wait(int *status)的用法:阻塞函数,等待任意一个子进程的返回. *wait(NULL):对子进程的结束状态不关心 ... 
- 执行PHP -m报错Xdebug MUST be loaded as a Zend extension
			Xdebug扩展安装后执行PHP -m报错: <br /><b>Warning</b>: Xdebug MUST be loaded as a Zend exten ... 
- AngularJs 中ngModel绑定HTML5 date数据同步问题
			以下代码例子中,直接将date类型的input标签与ng-model对应的变量绑定,会出现内存数据和页面数据不一致的问题.虽然AngularJS是双向数据绑定,但是如果用下面的方法,在页面更新date ... 
- KVC解析
			• 阅读 valueForKey (总体规划,先找相关方法,再找相关变量) 1.先是找相关方法,如果方法找不到 2.那么去判断 1 2 3 + (BOOL)accessInstanceVariab ... 
- F. Moving On
			http://codeforces.com/gym/102222/problem/F fory #include<bits/stdc++.h> using namespace std; t ... 
- E. Tree Painting(树形换根dp)
			http://codeforces.com/contest/1187/problem/E 分析:问得分最高,实际上就是问以哪个节点出发得到的分数最多,而呈现成代码形式就变成了换根,max其得分!!!而 ... 
- Mate20 pro实现H265 (HEVC)实时硬件编码
			谁能告诉我手机上用H265实时编码有什么鸟用? 一.先看看手机支持哪些codec ALL_CODECS REGULAR_CODECS mine-type 选择mime-type为video/hevc, ... 
- node-sass 安装失败的各种坑
			开始的时候引入别人的一个项目 npm install npm run dev 启动项目 报错 > node build/dev-server.js Listening at http://loc ... 
- 史无前例的KDD 2014大会记
			2014大会记" title="史无前例的KDD 2014大会记"> 作者:蒋朦 微软亚洲研究院实习生 创造多项纪录的KDD 2014 ACM SIGKDD 国际会 ... 
- svn merge   Property conflicts
			svn merge代码的时候,出现Property conflicts的解决方案.可以参考:http://stackoverflow.com/questions/23677286/conflict-w ... 
