DFS
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
vector <int>pre, mid;
struct Node {
int data;
Node* left;
Node* right;
Node() {
left = NULL;
right = NULL;
}
};
int M, N;
Node * build(int PL, int PR, int ML, int MR)
{
if (PL > PR)
{
return NULL;
}
int x = pre[PL];
int ind = -1;
for (int i = ML; i <= MR; i++)
{
if (mid[i] == x)
{
ind = i;
break;
}
}
int leftnum = ind - ML;
Node* root = new Node();
root->data = x;
root->left = build(PL+1, PL+leftnum, ML, ML+leftnum-1);
root->right = build(PL + leftnum+1,PR, ML + leftnum+1,MR);
return root;
}
vector<int>path;
vector <int>temp;
bool DFS(Node *root,int x)
{
if (root == NULL)
return false;
temp.push_back(root->data);
if (root->data == x)
{
path = temp;
temp.pop_back();
return true;
}
bool f = BFS(root->left, x) || BFS(root->right, x);
temp.pop_back();
return(f);
}
int main()
{
cin >>M>> N;
map<int, bool>isin;
for (int i = 0; i < N; i++)
{
int t; cin >> t;
mid.push_back(t);
}
for (int i = 0; i < N; i++)
{
int t; cin >> t;
pre.push_back(t);
}
Node *root = build(0, N - 1, 0, N - 1);
for (int i = 0; i < M; i++)
{
if (i != 0)printf("\n");
int a, b;
scanf("%d %d", &a, &b);
vector<int>pa, pb;
bool fa=BFS(root, a);
pa = path;
path.clear();
bool fb = DFS(root, b);
pb = path;
path.clear();
if (fa&&fb)
{
int ind =-1;
for (int j = 0; j < pa.size()&&j<pb.size(); j++)
{
if (pa[j] == pb[j])
{
ind = j;
}
else {
break;
}
}
if (pa[ind] != a && pa[ind] != b)
{
printf("LCA of %d and %d is %d.", a, b, pa[ind]);
}
else if (pa[ind] == a)
{
printf("%d is an ancestor of %d.", a, b);
}
else {
printf("%d is an ancestor of %d.", b, a);
}
}
else if (!fa && !fb)
{
printf("ERROR: %d and %d are not found.", a,b);
}
else if (fa && !fb)
{
printf("ERROR: %d is not found.", b);
}
else {
printf("ERROR: %d is not found.", a);
}
}
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
vector <int>pre, mid;
struct Node {
int data;
Node* left;
Node* right;
Node() {
left = NULL;
right = NULL;
}
};
int M, N;
map<int, int> hasin;
map<int, int> pos;
int myfind(int PL, int PR, int ML, int MR,int midx,int midy)
{
int root = pre[PL];
int index = -1;
for (int i = ML; i <= MR; i++)
{
if (root == mid[i])
{
index = i;
break;
}
}
int leftnum = index - ML;
if (midx <= index && index <= midy)
{
return root;
}
else if (midx < index&&midy < index)
{
return myfind(PL+1, PL+leftnum, ML, ML+leftnum-1, midx, midy);
}
else if (midx > index&&midy > index){
return myfind(PL + leftnum+1, PR, ML + leftnum+1, MR, midx, midy);
}
}
int main()
{
cin >>M>> N;
map<int, bool>isin;
for (int i = 0; i < N; i++)
{
int t; cin >> t;
hasin[t]++;
mid.push_back(t);
pos[t] = i;
}
for (int i = 0; i < N; i++)
{
int t; cin >> t;
pre.push_back(t);
}
for (int i = 0; i < M; i++)
{
if (i != 0)printf("\n");
int a, b;
scanf("%d %d", &a, &b);
if (hasin[a] > 0 && hasin[b] > 0)
{
int x = pos[a], y = pos[b];
int num=myfind(0,N-1,0,N-1,min(x,y),max(x,y));
if (num == a)
{
printf("%d is an ancestor of %d.", a,b);
}else if(num==b)
printf("%d is an ancestor of %d.", b, a);
else
printf("LCA of %d and %d is %d.", a, b,num);
}
else if (hasin[a] == 0 && hasin[b] == 0)
printf("ERROR: %d and %d are not found.", a, b);
else if (hasin[a] > 0 && hasin[b] == 0)
printf("ERROR: %d is not found.", b);
else
printf("ERROR: %d is not found.", a);
}
return 0;
}
还在想是否放弃用中序寻找根与两者关系转而用前序,然后突然想起来超时应该是因为中间对root的的查找使得时间复杂度从log上升到了Nlogn,并且其实我做了hash映射的,直接pos[root]查找就行。果然就成功了。
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
vector <int>pre, mid;
struct Node {
int data;
Node* left;
Node* right;
Node() {
left = NULL;
right = NULL;
}
};
int M, N;
map<int, int> hasin;
map<int, int> pos;
int myfind(int PL, int PR, int ML, int MR,int midx,int midy)
{
int root = pre[PL];
int index = pos[root];
int leftnum = index - ML;
if (midx <= index && index <= midy)
{
return root;
}
else if (midx < index&&midy < index)
{
return myfind(PL+1, PL+leftnum, ML, ML+leftnum-1, midx, midy);
}
else if (midx > index&&midy > index){
return myfind(PL + leftnum+1, PR, ML + leftnum+1, MR, midx, midy);
}
}
int main()
{
cin >>M>> N;
map<int, bool>isin;
for (int i = 0; i < N; i++)
{
int t; cin >> t;
hasin[t]++;
mid.push_back(t);
pos[t] = i;
}
for (int i = 0; i < N; i++)
{
int t; cin >> t;
pre.push_back(t);
}
for (int i = 0; i < M; i++)
{
if (i != 0)printf("\n");
int a, b;
scanf("%d %d", &a, &b);
if (hasin[a] > 0 && hasin[b] > 0)
{
int x = pos[a], y = pos[b];
int num=myfind(0,N-1,0,N-1,min(x,y),max(x,y));
if (num == a)
{
printf("%d is an ancestor of %d.", a,b);
}else if(num==b)
printf("%d is an ancestor of %d.", b, a);
else
printf("LCA of %d and %d is %d.", a, b,num);
}
else if (hasin[a] == 0 && hasin[b] == 0)
printf("ERROR: %d and %d are not found.", a, b);
else if (hasin[a] > 0 && hasin[b] == 0)
printf("ERROR: %d is not found.", b);
else
printf("ERROR: %d is not found.", a);
}
return 0;
}