Nearest Common Ancestors LCA的基本入门
来源:互联网 发布:淘宝1920图片显示不全 编辑:程序博客网 时间:2024/05/29 15:28
Nearest Common Ancestors
Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 15922Accepted: 8528
Description
A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:
In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. 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 of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 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 common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of 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 in a tree.
In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. 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 of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 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 common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of 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 in a tree.
Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case starts with a line containing an integer N , the number of nodes in a tree, 2<=N<=10,000. The nodes are labeled with integers 1, 2,..., N. Each of the next N -1 lines contains a pair of integers that represent an edge --the first integer is the parent node of the second integer. Note that a tree with N nodes has exactly N - 1 edges. The last line of each test case contains two distinct integers whose nearest common ancestor is to be computed.
Output
Print exactly one line for each test case. The line should contain the integer that is the nearest common ancestor.
Sample Input
2161 148 510 165 94 68 44 101 136 1510 116 710 216 38 116 1216 752 33 43 11 53 5
Sample Output
43
这是我第一次学习LCA的,我本来是要想要做双联通的问题,结果发现那道题目都用到了LCA,而我连LCA是啥都
不知道,惭愧,实在水城了渣渣,代码很简单,画个图分析下即可
#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define M 10010int far[M];int Lca(int a,int b){ if(a==b) return a; int w; while(a) { w=far[a]; far[a]=0; a=w; } for(;b;b=far[b]) { if(far[b]==0) return b; } return 0;}int main(){ int i,j,k,n; int T; int a,b; scanf("%d",&T); while(T--) { scanf("%d",&n); memset(far,0,sizeof(far)); for(i=1;i<n;i++) { scanf("%d%d",&a,&b); far[b]=a; } scanf("%d%d",&a,&b); int f=Lca(a,b); printf("%d\n",f); } return 0;}
Source
0 0
- Nearest Common Ancestors LCA的基本入门
- Nearest Common Ancestors--LCA
- 1330 Nearest Common Ancestors //LCA
- POJ1330 Nearest Common Ancestors[LCA]
- 【POJ1330】Nearest Common Ancestors(LCA)
- poj 1330 Nearest Common Ancestors(LCA入门题)
- POJ 1330 Nearest Common Ancestors lca入门题
- POJ 1330 Nearest Common Ancestors 倍增算法的LCA
- [PKU 1330]Nearest Common Ancestors(LCA)
- poj Nearest Common Ancestors(LCA+Tarjan)
- POJ-1330 Nearest Common Ancestors【LCA】
- POJ 1330 Nearest Common Ancestors [LCA+RMQ]
- POJ 1330Nearest Common Ancestors LCA模板
- poj 1330 Nearest Common Ancestors(LCA)
- (简单LCA) Nearest Common Ancestors(P1330)
- POJ 1330 Nearest Common Ancestors (LCA)
- poj 1330 Nearest Common Ancestors (LCA)
- poj 1330 Nearest Common Ancestors(LCA模板)
- sql server数据库改名
- 孔祥成
- 1062. Talent and Virtue (25)
- 如何在32位ubuntu11.10 下编译android 4.0.1源码和goldfish内核
- Thu Feb 13 16:30:25 CST 2014
- Nearest Common Ancestors LCA的基本入门
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
- Single Image Dehazing Algorithm Based on Sky Region Segmentation(2013)
- 如何修改Eclipse的背景颜色
- UIAppearance的使用
- 对当前的学习生活工作的反思和总结
- QEMU, KVM, QEMU-KVM 和 Goldfish
- NYOJ 464 Cookies
- HDU 3579 Hello Kiki 解同余方程组