20170713
来源:互联网 发布:数据 陈中源 编辑:程序博客网 时间:2024/06/05 08:16
T2:
已知一棵 n 个节点的有根树。有 m 个询问。每个询问给出了一对节点的编号 x 和 y ,询问 x 与 y 的祖孙关系。
输入
10
234 -1
12 234
13 234
14 234
15 234
16 234
17 234
18 234
19 234
233 19
5
234 233
233 12
233 13
233 15
233 19
输出
1lca模板
0
0
0
2
#include<bits/stdc++.h>using namespace std;int first[40005],father[40005],n,m,x,y,ans,tot;bool vis[40005];int f[40005][21],disl[40005];struct node{int next,to;}e[80005];inline void inser(int x,int y){tot++;e[tot].next=first[x];first[x]=tot;e[tot].to=y;}inline void dfs(int root){vis[root]=true;for(int u=first[root];u;u=e[u].next){int to=e[u].to;if(!vis[to]) disl[to]=disl[root]+1,dfs(to);}}int LCA(int x,int y){if(disl[x]<disl[y]) swap(x,y);int foot=disl[x]-disl[y];for(int i=18;i>=0;--i)if(foot>=(1<<i)) foot-=(1<<i),x=f[x][i];if(x==y) return x;for(int i=18;i>=0;--i)if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];return f[x][0];}int main(){//freopen("tree.in","r",stdin);//freopen("tree.out","w",stdout);scanf("%d",&n);int root=0;for(int i=1;i<=n;++i){scanf("%d%d",&x,&y);if(y==-1) root=x;else f[x][0]=y,inser(x,y),inser(y,x);}memset(disl,0,sizeof(disl));disl[root]=1;dfs(root);for(int i=1;i<=19;++i)for(int j=1;j<=40000;++j)if(f[j][i-1]) f[j][i]=f[f[j][i-1]][i-1];scanf("%d",&m);for(int i=1;i<=m;++i){scanf("%d%d",&x,&y);int lca=LCA(x,y);if(x==y) cout<<"0"<<endl;else if(lca==x) cout<<"1"<<endl;else if(lca==y) cout<<"2"<<endl;else cout<<"0"<<endl;} return 0;}
阅读全文
0 0
- 20170713
- 20170713
- 20170713
- 20170713智慧运营平台工作
- 一起来讨论讨论Java多线程技术-20170713
- 20170713——第二次修改PCB经验总结
- 20170713_合并有序链表_u
- CSDN日报20170713——《程序人生,终究是起承转合》
- CSDN日报20170713——《程序人生,终究是起承转合》
- Effective c++之Item 26: 只要有可能就推迟变量定义
- PHP-变量(一)(变量赋值、判断变量是否为空,变量删除,可变变量等)
- 排序相关——二维数组中的查找
- Dlib人脸检测及关键点描述的python实现
- 设计模式原则(4):接口隔离原则
- 20170713
- 如何拆分你的微服务架构?
- Ajax技术---Ajax技术原理,自己写一个Ajax
- reids centos7 安装
- Maven的pom.xml文件结构之基本配置parent和继承结构
- C++ map使用
- 并发与并行区别
- OpenCV学习(二、写视频)
- Java并发编程总结