博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1330 Nearest Common Ancestors (LCA)
阅读量:7022 次
发布时间:2019-06-28

本文共 3998 字,大约阅读时间需要 13 分钟。

Nearest Common Ancestors
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16530   Accepted: 8839

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. 

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

Source

 

LCA在线算法模板题:

1 //3304K    47MS    C++    1778B    2014-04-15 15:45:4 2 #include
3 #include
4 #include
5 #define N 10005 6 struct node{ 7 int u,v; 8 int next; 9 } edge[2*N];10 int n,num,u,v;11 int set[2*N],dep[2*N];12 int rank[N],head[N],vis[N];13 int dp[2*N][30],in[N]; 14 void addedge(int u,int v)15 {16 edge[num].u=u;17 edge[num].v=v;18 edge[num].next=head[u];19 head[u]=num++;20 }21 int Min(int a,int b)22 {23 return dep[a]
y) return set[RMQ(y,x)];59 else return set[RMQ(x,y)];60 }61 int main(void)62 {63 int t;64 scanf("%d",&t);65 while(t--)66 {67 scanf("%d",&n);68 memset(in,0,sizeof(in));69 memset(head,-1,sizeof(head));70 num=0;71 for(int i=1;i

 

Tarjan 离线算法:

1 //1036K    79MS    C++    1243B    2014-04-15 16:38:17 2 #include
3 #include
4 #include
5 #define N 10005 6 using namespace std; 7 vector
V[N]; 8 int set[N]; 9 int vis[N];10 int dp[N][30];11 int n,u0,v0,ans;12 void init()13 {14 for(int i=0;i<=n;i++) V[i].clear();15 memset(vis,0,sizeof(vis)); 16 }17 int find(int x)18 {19 if(set[x]!=x) set[x]=find(set[x]);20 return set[x];21 }22 void merge(int a,int b)23 {24 int x=find(a);25 int y=find(b);26 set[y]=x;27 }28 void LCA(int u)29 {30 set[u]=u;31 vis[u]=1;32 if(u==u0 && vis[v0]){33 ans=find(v0);34 }35 else if(u==v0 && vis[u0]){36 ans=find(u0);37 }38 for(int i=0;i

 

 

转载于:https://www.cnblogs.com/GO-NO-1/p/3666506.html

你可能感兴趣的文章
yum 本地源 配置
查看>>
只是留个记录好复制
查看>>
如何设置“系统偏好设置”的快捷键
查看>>
脚本test
查看>>
用ntpdate从时间服务器更新时间[Centos时间同步]
查看>>
第二天,仔细学习了下:common.inc.php(Discuz6.1.0核心文件)02
查看>>
手工kill掉VNC进程的故障处理
查看>>
python编程练习-字符串移位练习题
查看>>
python---list列表、元组
查看>>
LOG_ARCHIVE_CONFIG
查看>>
oracle 11gR2启用对sys用户操作行为的审计
查看>>
ActionScript3.0 AIR 透明背景+拖动功能
查看>>
C# winform combobox控件中子项加删除按钮(原创)
查看>>
我的友情链接
查看>>
网络工程师眼中的docker
查看>>
十八般武艺之Nginx踩坑总结
查看>>
为什么有人说组装机质量不好
查看>>
本地自旋锁与信号量/多服务台自旋队列-spin wait风格的信号量
查看>>
理想是自欺欺人的好东西
查看>>
我的友情链接
查看>>