The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.
Given any two nodes in a binary tree, you are supposed to find their LCA.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 1,000), the number of pairs of nodes to be tested; and N (≤ 10,000), the number of keys in the binary tree, respectively. In each of the following two lines, N distinct integers are given as the inorder and preorder traversal sequences of the binary tree, respectively. It is guaranteed that the binary tree can be uniquely determined by the input sequences. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int.
Output Specification:
For each given pair of U and V, print in a line LCA of U and V is A.
if the LCA is found and A
is the key. But if A
is one of U and V, print X is an ancestor of Y.
where X
is A
and Y
is the other node. If U or V is not found in the binary tree, print in a line ERROR: U is not found.
or ERROR: V is not found.
or ERROR: U and V are not found.
.
Sample Input:
6 8
7 2 3 4 6 5 1 8
5 3 7 2 6 4 8 1
2 6
8 1
7 9
12 -3
0 8
99 99
Sample Output:
LCA of 2 and 6 is 3.
8 is an ancestor of 1.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.
作者
CHEN, Yue
单位
浙江大学
代码长度限制
16 KB
时间限制
1000 ms
内存限制
64 MB
大意:m对需要查验的点,n为树中点的数目。给出二叉树的中序和先序遍历,判断u、v的最近公共节点。
吾:算法思想:先建树,然后遍历每个点,得到每个点的所有父节点也就是路径,然后比较u和v的路径找到最近的祖先节点。样例输出正常,但是耗时太久,内存占用太大。
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
vector<int> pre, in,v;
map<int, bool> mp;
struct node
{
int data;
node *left, *right;
};
node* buildTree(node* root, int preleft, int preright, int inleft, int inright)
{
if (inleft>inright)
{
return NULL;
}
if (root==NULL)
{
root = new node();
root->data = pre[preleft];
root->left = root->right = NULL;
}
int i = 0;
while (in[i]!=pre[preleft])
{
i++;
}
root->left = buildTree(root->left,preleft+1,preleft+i-inleft,inleft,i-1);
root->right = buildTree(root->right, preleft + i + 1, preright, i + 1, inright);
return root;
}
bool visit[10060];
vector<int> path[10060], temppath;
void dfs(node* root)
{
if (root==NULL)
{
return;
}
if (visit[root->data]==false)
{
path[root->data] = temppath;
visit[root->data] = true;
}
temppath.push_back(root->data);
if (root->left!=NULL)
{
dfs(root->left);
temppath.pop_back();
}
if (root->right!=NULL)
{
dfs(root->right);
temppath.pop_back();
}
}
int main()
{
//freopen("in.txt","r", stdin);
int m, n;
cin >> m >> n;
in.resize(n), pre.resize(n);
for (int i = 0; i < n; i++)
{
cin >> in[i];
mp[in[i]] = true;
}
for (int i = 0; i < n; i++)
{
cin >> pre[i];
}
node* root = NULL;
root=buildTree(root, 0, n-1, 0, n-1);
dfs(root);
//query
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
//找不到的情况
if (mp[u]==false&&mp[v]==false)
{
printf("ERROR: %d and %d are not found.\n", u, v);
continue;
}
else if(mp[u]==false||mp[v]==false)
{
printf("ERROR: %d is not found.\n", mp[u] == false ? u : v);
continue;
}
//其中一个是另个一祖先的情况
/*if (find(path[u].begin(),path[u].end(),v)!=path[u].end())
{
printf("%d is an ancestor of %d.\n", v, u);
continue;
}
else if(find(path[v].begin(), path[v].end(), u) != path[v].end())
{
printf("%d is an ancestor of %d.\n", u, v);
continue;
}*/
/*bool flag = false;
for (auto c:path[u])
{
if (c==v)
{
printf("%d is an ancestor of %d.\n", v, u);
flag = true;
break;
}
}
for (auto c:path[v])
{
if (c==u)
{
printf("%d is an ancestor of %d.\n", u, v);
flag = true;
break;
}
}
if (flag)
{
continue;
}*/
//最后一种情况 两者没有直接关系,找最近祖先
int ans = 0;
for (int j = 0; j < min(path[u].size(),path[v].size()); j++)
{
if (path[u][j]!=path[v][j])
{
ans = path[u][j - 1];
break;
}
ans = path[u][j];
}
printf("LCA of %d and %d is %d.\n", u, v, ans);
continue;
}
return 0;
}
柳:
#include <iostream>
#include <map>
#include <vector>
using namespace std;
vector<int> pre, in;
map<int, int> pos;
void lca(int preleft, int inleft, int inright, int u, int v)
{
if (inleft>inright)
{
return;
}
int i = pos[pre[preleft]];
if (pos[u] < i&&pos[v] < i)
{
lca(preleft + 1, inleft, i - 1, u, v);
}
else if ((pos[u]<i&&pos[v]>i) || (pos[v]<i&&pos[u]>i))
{
printf("LCA of %d and %d is %d.\n", u, v, pre[preleft]);
}
else if (pos[v] > i&&pos[u] > i)
{
lca(preleft + i - inleft + 1, i + 1, inright, u, v);
}
else if (pre[preleft] == u)// && pos[v] >= inleft && pos[v] <= inright
{
printf("%d is an ancestor of %d.\n", u, v);
}
else if (pre[preleft] == v)//&& pos[u] >= inleft && pos[u] <= inright
{
printf("%d is an ancestor of %d.\n", v, u);
}
//这是我的原代码:有两个测试点有问题,我想到了可能这些情况的先后顺序有关,写的时候我想那种情况在前在后无所谓,只要考虑全了就好。
//但是如果两种情况有重叠的部分,谁先谁后就很重要了,但这也只是猜想
//我的这种情况枚举顺序准确有什么问题,还没发现
//if ((pos[u]<i&&pos[v]>i) || (pos[v]<i&&pos[u]>i))
//{
// printf("LCA of %d and %d is %d.\n", u, v, pre[preleft]);
//}
//else if (pre[preleft] ==pos[ u] || pre[preleft] == v)
//{
// if (pre[preleft] == u)// && pos[v] >= inleft && pos[v] <= inright
// {
// printf("%d is an ancestor of %d.\n", u, v);
// }
// if (pre[preleft ]== v)//&& pos[u] >= inleft && pos[u] <= inright
// {
// printf("%d is an ancestor of %d.\n", v, u);
// }
//}
//else if ((pos[u] < i&&pos[v] < i) || (pos[v] > i&&pos[u] > i))
//{
// if (pos[u] < i&&pos[v] < i)
// {
// lca(preleft + 1, inleft, i - 1, u, v);
// }
// if (pos[v] > i&&pos[u] > i)
// {
// lca(preleft + i - inleft + 1, i + 1, inright, u, v);
// }
//}
return;
}
int main()
{
//freopen("in.txt", "r", stdin);
int m, n;
cin >> m >> n;
pre.resize(n + 1), in.resize(n + 1);
for (int i = 1; i <= n; i++)//这里必须是从1开始的,因为map的默认值只能是0,
//如果从0开始,那么中序序列中第一个数的pos值总是0,
//在判断是否能found的时候会误判,即那个数是存在的,
//但是因为它第一个数,pos值是0而误判它不存在。
//所以pre、in也resize为n+1.
{
cin >> in[i];
pos[in[i]] = i;
}
for (int i = 1; i <= n; i++)
{
cin >> pre[i];
}
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
if (pos[u] == 0 && pos[v] == 0)
{
printf("ERROR: %d and %d are not found.\n", u, v);
}
else if (pos[u] == 0 || pos[v] == 0)
{
printf("ERROR: %d is not found.\n", pos[u] == 0 ? u : v);
}
else
{
lca(1, 1, n, u, v);
}
}
return 0;
}
二刷:注意这句话,所有的值都是在int范围内,可能会非常大,所以pos用vector不可以,就算大小开到10006也不行,n<10^4,n说的是点的个数,不是值的大小,所以必须用map。
Lca算法技巧性强,很有意思,要get。
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
vector<int> pre, in;
unordered_map<int, int> pos;
void lca(int preleft, int inleft, int inright, int u, int v)
{
if (inleft>inright)
{
return;
}
int root = pos[pre[preleft]];
if (pos[u]<root&&pos[v]<root)
{
lca(preleft + 1, inleft, root - 1, u, v);
}
else if((pos[u]<root&&pos[v]>root)||(pos[u]>root&&pos[v]<root))
{
printf("LCA of %d and %d is %d.\n", u, v, pre[preleft]);
}
else if(pos[u]>root&&pos[v]>root)
{
lca(preleft + root - inleft + 1, root + 1, inright, u, v);
}
else if(pre[preleft]==u)
{
printf("%d is an ancestor of %d.\n", u, v);
}
else if(pre[preleft]==v)
{
printf("%d is an ancestor of %d.\n", v, u);
}
}
int main()
{
//freopen("in.txt", "r", stdin);
int n, m;
cin >> m >> n;
pre.resize(n + 1), in.resize(n + 1);
for (int i = 1; i <= n; i++)
{
cin >> in[i];
pos[in[i]] = i;
}
for (int i = 1; i <= n; i++)
{
cin >> pre[i];
}
//query
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
if (pos[u]==0&&pos[v]==0)
{
printf("ERROR: %d and %d are not found.\n", u, v);
}
else if (pos[u]==0||pos[v]==0)
{
printf("ERROR: %d is not found.\n", pos[u] == 0 ? u : v);
}
else
{
lca(1, 1, n, u, v);
}
}
}