B1151 LCA in a Binary Tree (30分)
//。。。待补
LCA没学,具体操作如下
node* LCA(tree &T,int u,int v)
{
if(T==NULL)return NULL;
if(T->val==u || T->val==v) return T;
tree l=LCA(T->lchild,u,v);
tree r=LCA(T->rchild,u,v);
if(l!=NULL && r!=NULL)return T;
return l==NULL?r:l;
}
反面教材:模拟
//2,3段错误,25[汪柴]。。
- 建树,广搜一遍记录pre路径,从要查找的结点往上递归把两条找出来,找第一个不相同的的数字的前一位就是LCA
- 假如到某一条尾部还没找到,说明短的那个,是长的那个的LCA
我寻思着建树的时候就可以把前驱结点记录下来了啊/(ㄒoㄒ)/~~
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#define lowbit(i)((i)&(-i))
using namespace std;
typedef long long ll;
const int MAX=10001;
const int INF=0x3f3f3f3f;
const int MOD=1000000007;
const int SQR=633;
struct node
{
node* lchild;
node* rchild;
int data;
};
int in[MAX],pre[MAX];
int book[MAX],temp[MAX];
int n,m,toproot;
vector<int>temppath1,temppath2,temppath;
node* create(int inl,int inr,int prel,int prer)
{
if(prel>prer)
return NULL;
int i;
node* root=new node;
root->data=pre[prel];
for(i=inl;i<=inr;i++)
{
if(in[i]==pre[prel])
break;
}
int leftnum=i-inl;//不是i-1!!!!
root->lchild=create(inl,i-1,prel+1,prel+leftnum);
root->rchild=create(i+1,inr,prel+leftnum+1,prer);
return root;
}
void bfs(node* root)
{
temp[toproot]=toproot;
queue<node*>q;
q.push(root);
while(!q.empty())
{
node* top=q.front();
q.pop();
book[top->data]=1;
if(top->lchild!=NULL)
{
q.push(top->lchild);
temp[top->lchild->data]=top->data;
}
if(top->rchild!=NULL)
{
q.push(top->rchild);
temp[top->rchild->data]=top->data;
}
}
}
void dfs(int top,int v)//top,当前顶点
{
if(v==top)
{
temppath.push_back(v);
return ;
}
dfs(top,temp[v]);
temppath.push_back(v);
}
int main()
{
memset(book,0,sizeof(book));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
scanf("%d",&in[i]);
for(int i=1;i<=m;i++)
scanf("%d",&pre[i]);
node* root=create(1,m,1,m);
toproot=root->data;
bfs(root);
for(int i=0;i<n;i++)
{
int x,y,j;
scanf("%d%d",&x,&y);
if(book[x]==1&&book[y]==0)
printf("ERROR: %d is not found.\n",y);
else if(book[x]==0&&book[y]==1)
printf("ERROR: %d is not found.\n",x);
else if(book[x]==0&&book[y]==0)
printf("ERROR: %d and %d are not found.\n",x,y);
else
{
dfs(toproot,x);
temppath1=temppath;
temppath.clear();
dfs(toproot,y);
temppath2=temppath;
temppath.clear();
int len=min(temppath1.size(),temppath2.size());
for(j=0;j<len;j++)
{
if(temppath1[j]!=temppath2[j])
break;
}
if(j==len)
{
if(temppath1.size()==len)
printf("%d is an ancestor of %d.\n",temppath1[temppath1.size()-1],temppath2[temppath2.size()-1]);
else
printf("%d is an ancestor of %d.\n",temppath2[temppath2.size()-1],temppath1[temppath1.size()-1]);
}
else
printf("LCA of %d and %d is %d.\n",x,y,temppath1[j-1]);
}
}
return 0;
}
查阅网上的题解
- 题解1:不用建树~已知某个树的根结点,若a和b在根结点的左边,则a和b的最近公共祖先在当前子树根结点的左子树寻找,如果a和b在当前子树根结点的两边,在当前子树的根结点就是a和b的最近公共祖先,如果a和b在当前子树根结点的右边,则a和b的最近公共祖先就在当前子树的右子树寻找。中序加先序可以唯一确定一棵树,在不构建树的情况下,在每一层的递归中,可以得到树的根结点,在此时并入lca算法可以确定两个结点的公共祖先
#include <iostream>
#include <vector>
#include <map>
using namespace std;
map<int, int> pos;
vector<int> in, pre;
void lca(int inl, int inr, int preRoot, int a, int b) {
if (inl > inr) return;
int inRoot = pos[pre[preRoot]], aIn = pos[a], bIn = pos[b];
if (aIn < inRoot && bIn < inRoot)
lca(inl, inRoot-1, preRoot+1, a, b);
else if ((aIn < inRoot && bIn > inRoot) || (aIn > inRoot && bIn < inRoot))
printf("LCA of %d and %d is %d.\n", a, b, in[inRoot]);
else if (aIn > inRoot && bIn > inRoot)
lca(inRoot+1, inr, preRoot+1+(inRoot-inl), a, b);
else if (aIn == inRoot)
printf("%d is an ancestor of %d.\n", a, b);
else if (bIn == inRoot)
printf("%d is an ancestor of %d.\n", b, a);
}
int main() {
int m, n, a, b;
scanf("%d %d", &m, &n);
in.resize(n + 1), pre.resize(n + 1);
for (int i = 1; i <= n; i++) {
scanf("%d", &in[i]);
pos[in[i]] = i;
}
for (int i = 1; i <= n; i++) scanf("%d", &pre[i]);
for (int i = 0; i < m; i++) {
scanf("%d %d", &a, &b);
if (pos[a] == 0 && pos[b] == 0)
printf("ERROR: %d and %d are not found.\n", a, b);
else if (pos[a] == 0 || pos[b] == 0)
printf("ERROR: %d is not found.\n", pos[a] == 0 ? a : b);
else
lca(1, n, 1, a, b);
}
return 0;
}
- 题解2:LCA模板
#include<bits/stdc++.h>
using namespace std;
typedef struct node{
struct node *lchild;
struct node *rchild;
int val;
}node,*tree;
const int maxn=1e4+10;
int in[maxn],pre[maxn];
int m,n,cur;
int findRoot(int x)
{
for(int i=0;i<n;++i)
if(in[i]==x)return i;
return -1;
}
void build(tree &T,int left,int right)
{
if(right<left || cur>=n)return ;
int root=pre[cur];
cur++;
int index=findRoot(root);
T=new node();
T->val=root;
if(right==left) T->lchild=T->rchild=NULL;
else{
build(T->lchild,left,index-1);
build(T->rchild,index+1,right);
}
}
node* LCA(tree &T,int u,int v)
{
if(T==NULL)return NULL;
if(T->val==u || T->val==v) return T;
tree l=LCA(T->lchild,u,v);
tree r=LCA(T->rchild,u,v);
if(l!=NULL && r!=NULL)return T;
return l==NULL?r:l;
}
int main()
{
scanf("%d%d",&m,&n);
for(int i=0;i<n;i++)scanf("%d",&in[i]);
for(int i=0;i<n;i++)scanf("%d",&pre[i]);
tree T; cur=0;
build(T,0,n-1);
while(m--)
{
int u,v;scanf("%d%d",&u,&v);
int f1=1,f2=1;
//直接调用findRoot函数,如果返回-1即无此节点
if(findRoot(u)==-1)f1=0;
if(findRoot(v)==-1)f2=0;
if(!f1 && !f2)printf("ERROR: %d and %d are not found.\n",u,v);
else if(!f1)printf("ERROR: %d is not found.\n",u);
else if(!f2)printf("ERROR: %d is not found.\n",v);
else{
tree T2=LCA(T,u,v);
if(T2->val==u || T2->val==v)
printf("%d is an ancestor of %d.\n",T2->val==u?u:v,T2->val==u?v:u);
else printf("LCA of %d and %d is %d.\n",u,v,T2->val);
}
}
}