两个测试点超时
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;
}
版本4
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <unordered_map>
#include <algorithm>
using namespace std;
const int N = 1e4+5;
int pre[N], in[N];
int n, m;
unordered_map<int, int> mp;
struct tree{
int val;
tree* left, *right;
tree(int x){
val = x;
left = right = nullptr;
}
};
tree* create(int prel, int prer, int inl, int inr){
if(prel > prer) return nullptr;
int val = pre[prel];
tree* root = new tree(val);
int k = mp[val];
int numLeft = k - inl;
root->left = create(prel+1, prel+numLeft, inl, k-1);
root->right = create(prel+numLeft+1, prer, k+1, inr);
return root;
}
tree* LCA(tree* root, int p, int q){
if(root == nullptr || root->val == p || root->val == q) return root;
auto left = LCA(root->left, p, q);
auto right = LCA(root->right, p, q);
if(left && right) return root;
return left? left: right;
}
int main(){
scanf("%d%d", &m, &n);
for(int i = 0; i < n; i++){
scanf("%d", pre+i);
in[i] = pre[i];
}
sort(in, in+n);
for(int i =0; i < n; i++) mp[in[i]] = i;
tree* root = create(0, n-1, 0, n-1);
int u, v;
while(m--){
scanf("%d %d", &u, &v);
if(!mp.count(u) && !mp.count(v)) printf("ERROR: %d and %d are not found.\n", u, v);
else if(!mp.count(u)) printf("ERROR: %d is not found.\n", u);
else if(!mp.count(v)) printf("ERROR: %d is not found.\n", v);
else{
tree* t = LCA(root, u, v);
if(t->val == u) printf("%d is an ancestor of %d.\n", u, v);
else if(t->val == v) printf("%d is an ancestor of %d.\n", v, u);
else printf("LCA of %d and %d is %d.\n", u, v, t->val);
}
}
return 0;
}