poj 1330 Nearest Common Ancestors(lca st)
来源:互联网 发布:鬼魂探测器软件 编辑:程序博客网 时间:2024/06/05 16:21
Nearest Common Ancestors
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 29840 Accepted: 15228
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
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <map>#include <queue>using namespace std;typedef long long ll;const int N = 2e4+10, inf = 0x3f3f3f3f;int a[N], b[N];struct node{ int to, next, cap;}p[N*2];int head[N], cnt, in[N], tot, first[N], vis[N], dep[N], cur[N] ;void init(){ memset(head,-1,sizeof(head)); memset(in,0,sizeof(in)); memset(vis,0,sizeof(vis)); cnt=0, tot=0; return ;}void add(int u,int v){ p[cnt].to=v,p[cnt].next=head[u];head[u]=cnt++; p[cnt].to=u,p[cnt].next=head[v];head[v]=cnt++; return ;}void dfs(int u,int dist){ vis[u]=1,dep[++tot]=dist;cur[tot]=u, first[u]=tot; for(int i=head[u];i!=-1;i=p[i].next) { int v=p[i].to; if(!vis[v]) { dfs(v,dist+1); dep[++tot]=dist; cur[tot]=u; } } return ;}int dp[N][25];void rmq(int n){ for(int i=1;i<=n;i++) dp[i][0]=i; int k=(int)(1.0*log(1.0*n)/log(1.0*2)); for(int i=1;i<=k;i++) { for(int j=1;j+(1<<i)-1<=n;j++) { dp[j][i]=dep[dp[j][i-1]]<dep[dp[j+(1<<(i-1))][i-1]]?dp[j][i-1]:dp[j+(1<<(i-1))][i-1]; } } return ;}int lca(int x,int y){ int u=first[x],v=first[y]; if(u>v) swap(u,v); int k=(v-u+1); int h=(int)(1.0*log(1.0*k)/log(2.0)); k=h; return dep[dp[u][k]]<dep[dp[v-(1<<k)+1][k]]?cur[dp[u][k]]:cur[dp[v-(1<<k)+1][k]];}int main(){ int t; scanf("%d", &t); while(t--) { int n, x, y; scanf("%d", &n); init(); for(int i=1;i<n;i++) { scanf("%d %d", &x, &y); add(x,y); in[y]++; } int root; for(int i=1;i<=n;i++) { if(in[i]==0) { root=i; break; } } dfs(root,0); rmq(2*n-1); scanf("%d %d", &x, &y); printf("%d\n",lca(x,y)); } return 0;}
阅读全文
0 0
- poj 1330 Nearest Common Ancestors(lca st)
- poj 1330 Nearest Common Ancestors LCA tarjan/RMQ ST
- POJ-1330 Nearest Common Ancestors【LCA】
- POJ 1330 Nearest Common Ancestors [LCA+RMQ]
- POJ 1330Nearest Common Ancestors LCA模板
- poj 1330 Nearest Common Ancestors(LCA)
- POJ 1330 Nearest Common Ancestors (LCA)
- poj 1330 Nearest Common Ancestors (LCA)
- poj 1330 Nearest Common Ancestors(LCA模板)
- POJ 1330 Nearest Common Ancestors LCA
- POJ 1330 Nearest Common Ancestors LCA
- poj 1330 Nearest Common Ancestors LCA targin
- POJ 1330 Nearest Common Ancestors LCA题解
- poj 1330 Nearest Common Ancestors (LCA)
- Nearest Common Ancestors - POJ 1330 LCA
- 【LCA】 POJ 1330 Nearest Common Ancestors
- POJ 1330 Nearest Common Ancestors LCA
- POJ 1330 Nearest Common Ancestors (LCA)
- 序
- react-redux使用小结
- 51单片机中使用ucos ii的优缺点
- NYOJ 59-小明组织活动的任务(置换)
- 模态框总结
- poj 1330 Nearest Common Ancestors(lca st)
- HDU 6097 Mindis(计算几何)
- 刷题——Stall Reservations POJ
- JavaWeb-ssm框架搭建
- js打开本地exe程序
- power designer远程连接oracle数据库并导出E-R图
- C语言中restrict的作用
- 《Drools7.0.0.Final规则引擎教程》番外实例篇——默认条件的陷阱
- javaSE和JavaEE的区别?