博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【poj】lca模板题 poj1330
阅读量:6035 次
发布时间:2019-06-20

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

附题目链接:
Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u

 

 

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
 

 

才学了lca,于是找了一些lca的模板题来练练手,写的是在线算法,倍增lca。。具体看代码注释吧

【代码】:

1 #include
2 #include
3 #include
4 using namespace std; 5 struct edge{ 6 int u,v,next; 7 }e[10000 + 10]; 8 int head[10000 + 10],k = 1; 9 int deep[10000 + 5];10 int vis[10000 + 5];11 int p[10000][20 + 5];//p[i][j]表示第i点的第2^j个祖先12 int T;13 int n;14 void adde(int u,int v)15 {16 e[k].u = u;17 e[k].v = v;18 e[k].next = head[u];19 head[u] = k++;20 }21 void dfs(int u)22 {23 for(int i = head[u]; i ; i = e[i].next)24 {25 if(!deep[e[i].v])26 {27 deep[e[i].v] = deep[u] + 1;28 p[e[i].v][0] = u;29 dfs(e[i].v);30 }31 }32 }33 void init()34 {35 int i, j;36 for(j = 1; (1 << j) <= n; j++)37 {38 for(i = 1; i <= n; i++)39 {40 //if(p[i][j - 1] != -1)41 p[i][j] = p[p[i][j - 1]][j - 1];//i的第2^j-1的祖先的第2^j-1的祖先就是i的第2^j个祖先,有点绕,可画图看42 }43 }44 }45 int lca(int a,int b)46 {47 int i, j;48 if(deep[a] < deep[b])swap(a,b);49 for(i = 0; (1 << i) <= n; i++);50 i--;//找出最多跳的次数51 for(j = i; j >= 0; j--)52 if(deep[a] - (1 << j) >= deep[b])53 a = p[a][j];//跳到与b同一深度54 if(a == b)return a;//此情况是a,b同点55 for(j = i; j >= 0; j--)56 {57 if(p[a][j] != -1 && p[a][j] != p[b][j])//a,b的最远的不相等的祖先的父亲就是求得lca58 {59 a = p[a][j];60 b = p[b][j];//不断向上跳61 }62 }63 return p[a][0];//最后a的父亲便是lca64 }65 int main()66 {67 scanf("%d",&T);68 while(T--)69 {70 int root;71 memset(p,0,sizeof(p));72 memset(deep,0,sizeof(deep));73 memset(head,0,sizeof(head));74 memset(vis,0,sizeof(vis));75 //memset(e,0,sizeof(e));76 k = 1;77 scanf("%d",&n);78 for(int i = 1; i < n; i++)79 {80 int a,b;81 scanf("%d%d",&a,&b);82 p[b][0] = a;83 adde(a,b);84 if(p[a][0] == 0)85 root = a;86 }87 deep[root] = 1;88 dfs(root);89 init();90 int x,y;91 scanf("%d%d",&x,&y);92 printf("%d\n",lca(x,y));93 }94 95 96 return 0;97 }
View Code

最后想说的是,为什么会超时,似乎有点问题。。到时等理解更深后在来优化吧,代码没问题。就当做模板吧

转载于:https://www.cnblogs.com/kiritoghy/p/4689059.html

你可能感兴趣的文章
[面试题记录01]实现一个function sum达到一下目的
查看>>
这个季节的忧伤,点到为止
查看>>
mysql通过配置文件进行优化
查看>>
省级网站群建设关注点
查看>>
工作第四天之采集资源
查看>>
我的友情链接
查看>>
H3CS-WLAN、H3CSE-Security认证考试
查看>>
5.0中redis-cli的集群管理测试
查看>>
TFS 2012研发管理能力(5)
查看>>
四种LaunchMode及其使用场景
查看>>
通过vbs脚本备份数据-本地到异地
查看>>
tomcat介绍和安装
查看>>
UIButton的titleLabel不同状态字体判断
查看>>
我的友情链接
查看>>
杨泽业:wordpress在Nginx/Apache/IIS中的伪静态规则
查看>>
Python 中使用 MongoDB 存储爬虫数据
查看>>
WindowsServer 2008 AD搭建FTP隔离用户
查看>>
lmdb
查看>>
大文件如何传输,大文件的传输方式有哪些?
查看>>
docker的持久化存储和共享存储和网络架构
查看>>