两个测试点超时
22分
版本1
#include<bits/stdc++.h>
using namespace std;
struct node{
int x;
node* left;
node* right;
};
//在线建树很费时
void create(node* &root,int v){
if(root == NULL){
root = new node;
root->x = v;
root->left = root->right = NULL;
return;
}else if(root->x <= v){
create(root->right, v);
}else create(root->left, v);
}
//深搜
void dfs(node* root, int u, int v, int f){
if(root == NULL) return;
if(root->x == u){
printf("%d is an ancestor of %d.\n", u,v);
return;
}else if(root->x == v){
printf("%d is an ancestor of %d.\n", v,u);
return;
}else if(root->x > u && root->x < v){
if(f) swap(u,v); //交换过的,要换回来再输出,不然是反的
printf("LCA of %d and %d is %d.\n", u, v, root->x);
return;
}else if(root->x >u && root->x > v) dfs(root->left,u,v,f);
else dfs(root->right,u,v,f);
}
map<int, bool> mp;
int main(){
int n,m,x,y;
scanf("%d%d",&n,&m);
node* root=NULL;
for(int i=0;i<m;i++){
scanf("%d",&x);
mp[x]=true;
create(root, x);
}
for(int i=0;i<n;i++){
scanf("%d%d",&x,&y);
if(mp[x]==false && mp[y]==false) printf("ERROR: %d and %d are not found.\n", x, y);
else if(mp[x]==false) printf("ERROR: %d is not found.\n", x);
else if(mp[y]==false) printf("ERROR: %d is not found.\n", y);
else{
bool flag = false;
if(x > y){
flag = true;
swap(x, y);
}
dfs(root, x, y, flag);
}
}
return 0;
}
版本2
柳婼大神
#include <iostream>
#include <vector>
#include <map>
using namespace std;
map<int, bool> mp;
int main() {
int m, n, u, v, a;
scanf("%d %d", &m, &n);
vector<int> pre(n);
for (int i = 0; i < n; i++) {
scanf("%d", &pre[i]);
mp[pre[i]] = true;
}
for (int i = 0; i < m; i++) {
scanf("%d %d", &u, &v);
for(int j = 0; j < n; j++) {
a = pre[j];
if ((a >= u && a <= v) || (a >= v && a <= u)) break;
}
if (mp[u] == false && mp[v] == false)
printf("ERROR: %d and %d are not found.\n", u, v);
else if (mp[u] == false || mp[v] == false)
printf("ERROR: %d is not found.\n", mp[u] == false ? u : v);
else if (a == u || a == v)
printf("%d is an ancestor of %d.\n", a, a == u ? v : u);
else
printf("LCA of %d and %d is %d.\n", u, v, a);
}
return 0;
}
版本3
可以通过!
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+5;
map<int, bool> mp;
int pre[maxn],in[maxn];
struct node{
int x;
node* left;
node* right;
};
//离线建树好一点
void create(node* &root, int preL, int preR, int inL, int inR){
if(preL > preR ) return;
root = new node; //很关键
root->x = pre[preL];
root->left = root->right =NULL;
int k=inL;
while(in[k] != pre[preL]){
k++;
}
int tmp = k - inL;
create(root->left,preL+1, preL+tmp, inL, k-1);
create(root->right,preL+tmp+1, preR, k+1, inR);
}
void inorder(node* root){
if(root == NULL) return;
inorder(root->left);
cout<<root->x<<" ";
inorder(root->right);
}
//深搜
void dfs(node* root, int u, int v, int f){
if(root == NULL) return;
if(root->x == u){
printf("%d is an ancestor of %d.\n", u,v);
return;
}else if(root->x == v){
printf("%d is an ancestor of %d.\n", v,u);
return;
}else if(root->x > u && root->x < v){
if(f) swap(u,v); //交换过的,要换回来再输出,不然是反的
printf("LCA of %d and %d is %d.\n", u, v, root->x);
return;
}else if(root->x >u && root->x > v) dfs(root->left,u,v,f);
else dfs(root->right,u,v,f);
}
int main(){
int n,m,x,y;
node* root = NULL;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d",&x);
in[i]=x;
pre[i]=x;
mp[x]=true;
}
sort(in,in+m);
create(root,0, m-1, 0, m-1);
for(int i=0;i<n;i++){
scanf("%d%d",&x,&y);
if(mp[x]==false && mp[y]==false) printf("ERROR: %d and %d are not found.\n", x, y);
else if(mp[x]==false) printf("ERROR: %d is not found.\n", x);
else if(mp[y]==false) printf("ERROR: %d is not found.\n", y);
else{
bool flag = false;
if(x > y){
flag = true;
swap(x, y);
}
dfs(root, x, y, flag);
}
}
return 0;
}