【LCA倍增模板】【poj1330】最近公共祖先
来源:互联网 发布:新手手机编程软件 编辑:程序博客网 时间:2024/05/22 01:57
Nearest Common Ancestors
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 31027
Accepted: 15800
Description
A rooted tree is a well-known data structure in computerscience and engineering. An example is shown below:
In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8is the root of the tree. Node x is an ancestor of node y if node x is in thepath between the root and node y. For example, node 4 is an ancestor of node16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4,10, and 16 are the ancestors of node 16. Remember that a node is an ancestor ofitself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called acommon ancestor of two different nodes y and z if node x is an ancestor of nodey and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors ofnodes 16 and 7. A node x is called the nearest common ancestor of nodes y and zif x is a common ancestor of y and z and nearest to y and z among their commonancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node4 is nearer to nodes 16 and 7 than node 8 is.
For other examples, the nearest common ancestor of nodes 2 and 3 is node 10,the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest commonancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestorof z, then the nearest common ancestor of y and z is y.
Write a program that finds the nearest common ancestor of two distinct nodes ina tree.
Input
The input consistsof T test cases. The number of test cases (T) is given in the first line of theinput file. Each test case starts with a line containing an integer N , thenumber of nodes in a tree, 2<=N<=10,000. The nodes are labeled withintegers 1, 2,..., N. Each of the next N -1 lines contains a pair of integersthat represent an edge --the first integer is the parent node of the secondinteger. Note that a tree with N nodes has exactly N - 1 edges. The last lineof each test case contains two distinct integers whose nearest common ancestoris to be computed.
Output
Print exactly oneline for each test case. The line should contain the integer that is thenearest common ancestor.
Sample Input
2
16
1 14
8 5
10 16
5 9
4 6
8 4
4 10
1 13
6 15
10 11
6 7
10 2
16 3
8 1
16 12
16 7
5
2 3
3 4
3 1
1 5
3 5
Sample Output
4
3
(不翻译了不翻译了就是多组输入输出数据,每次x,y表示x是y的爸爸,最后一行询问)
#include<iostream>#include<queue>#include<cstdio>#include<algorithm>#define N 10050using namespace std;int T,n,m,tot;int head[N],next[N],v[N],depth[N],fa[N][21];void add_edge(int x,int y){ v[++tot]=y; next[tot]=head[x]; head[x]=tot;}queue<int> q;void bfs(int root){ q.push(root);depth[root]=1;fa[root][0]=root; while(!q.empty()) { int x=q.front();q.pop(); for (int i=head[x];i;i=next[i]) { int y=v[i];q.push(y); depth[y]=depth[x]+1; for (int j=1;j<=20;j++) fa[y][j]=fa[fa[y][j-1]][j-1]; } }}int lca(int x,int y){ if (depth[x]<depth[y]) swap(x,y); int d=depth[x]-depth[y]; for (int i=0;i<=20;i++) if ((1<<i)&d) x=fa[x][i]; for (int i=20;i>=0;i--) if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i]; return x==y?x:fa[x][0];}int main(){ cin>>T; while(T--) { cin>>n;tot=0; for (int i=0;i<n+10;i++) { head[i]=0;next[i]=0;v[i]=0;depth[i]=0; for (int j=0;j<=20;j++) fa[i][j]=0; } for (int i=0;i<n-1;i++) { int x,y; cin>>x>>y; fa[y][0]=x; add_edge(x,y); } int root; for (int i=1;i<=n;i++) if (fa[i][0] == 0) {root=i;break;} bfs(root); int a,b; cin>>a>>b; cout<<lca(a,b)<<endl; } return 0;}1 bfs记得把更新的点放在队列里
2 lca是要特判y是x祖先的时候
3 无根图要找根,也就是没有爸爸的点
- 【LCA倍增模板】【poj1330】最近公共祖先
- poj1330(LCA最近公共祖先)
- lca(最近公共祖先)倍增模板【pascal】
- LCA(最近公共祖先)倍增法模板及总结
- 【讲解+模板】最近公共祖先(LCA)(倍增)
- 最近公共祖先 LCA 倍增算法
- 最近公共祖先(LCA)---倍增法
- 最近公共祖先(LCA):倍增
- 最近公共祖先 LCA 倍增+Tarjan实现
- poj 1986 最近公共祖先 (lca 倍增)
- 最近公共祖先LCA倍增算法
- 倍增法求最近公共祖先 lca
- LCA(最近公共祖先)倍增算法
- 最近公共祖先 LCA 倍增算法
- 最近公共祖先LCA【模板】
- [模板]最近公共祖先LCA
- 【模板】lca 最近公共祖先
- POJ1330(最近公共祖先)
- 5.9运用if-else语句
- Vulkan规范:第五章 5.3
- Angular开发-关于组件之间的数据交互
- git 撤销修改
- Adaboost
- 【LCA倍增模板】【poj1330】最近公共祖先
- Java8 I/O源码-RandomAccessFile
- struts2跳转犯错“no result for action xxx and result success”
- 可判断数据中有多个数字只出现一次,其他所有数字都是成对出现的。然后输出这些数字。(使用位运算)
- 严蔚敏《数据结构》代码解析1
- iOS登录界面和注册界面
- 文章标题
- [译]Angular2新人常犯的5个错误
- 如何写一个RPC框架(三):服务注册与服务发现