PAT A1151 LCA in Binary Tree
利用树的前序和中序递归判定最小公共祖先~
直接根据两个序列递归处理~
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int N,M;
int pre[maxn],in[maxn];
unordered_map<int,int> pos;
void lca (int inL,int inR,int preRoot,int a,int b) {
if (inL>inR) return;
int inRoot=pos[pre[preRoot]];
int aIn=pos[a];
int bIn=pos[b];
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+,inR,preRoot+inRoot-inL+,a,b);
else if (aIn<inRoot&&bIn<inRoot)
lca (inL,inRoot-,preRoot+,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 () {
scanf ("%d %d",&M,&N);
for (int i=;i<=N;i++) {
scanf ("%d",&in[i]);
pos[in[i]]=i;
}
for (int i=;i<=N;i++)
scanf ("%d",&pre[i]);
int a,b;
for (int i=;i<M;i++) {
scanf ("%d %d",&a,&b);
if (pos[a]==&&pos[b]==)
printf ("ERROR: %d and %d are not found.\n",a,b);
else if (pos[a]==)
printf ("ERROR: %d is not found.\n",a);
else if (pos[b]==)
printf ("ERROR: %d is not found.\n",b);
else lca (,N,,a,b);
}
return ;
}
也可以根据树建图,跑lca算法
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node {
int data;
node * left;
node * right;
};
int M,N;
int pre[maxn],in[maxn];
unordered_map<int,int> pos;
int father[maxn*];
int depth[maxn*];
node * create (int preL,int preR,int inL,int inR) {
if (preL>preR) return NULL;
node * root=new node;
root->data=pre[preL];
int k;
for (k=inL;k<=inR;k++)
if (in[k]==pre[preL]) break;
int numLeft=k-inL;
root->left=create(preL+,preL+numLeft,inL,k-);
if (root->left!=NULL) father[root->left->data]=root->data;
root->right=create(preL+numLeft+,preR,k+,inR);
if (root->right!=NULL) father[root->right->data]=root->data;
return root;
}
void bfs (node * root) {
queue<node *> q;
q.push(root);
depth[root->data]=;
while (!q.empty()) {
node * now=q.front();
q.pop();
if (now->left) {q.push(now->left);depth[now->left->data]=depth[now->data]+;}
if (now->right) {q.push(now->right);depth[now->right->data]=depth[now->data]+;}
}
}
void lca (int u,int v) {
int tu=u;
int tv=v;
while (depth[tu]<depth[tv]) {
tv=father[tv];
}
while (depth[tu]>depth[tv]) {
tu=father[tu];
}
while (tu!=tv) {
tu=father[tu];
tv=father[tv];
}
if (tu==u) printf ("%d is an ancestor of %d.\n",u,v);
else if (tu==v) printf ("%d is an ancestor of %d.\n",v,u);
else printf ("LCA of %d and %d is %d.\n",u,v,tu);
}
int main () {
scanf ("%d %d",&M,&N);
for (int i=;i<=N;i++) father[i]=i;
for (int i=;i<=N;i++) {
scanf ("%d",&in[i]);
pos[in[i]]=i;
}
for (int i=;i<=N;i++)
scanf ("%d",&pre[i]);
node * root=create(,N,,N);
bfs(root);
int u,v;
for (int i=;i<M;i++) {
scanf ("%d %d",&u,&v);
if (pos[u]==&&pos[v]==) printf ("ERROR: %d and %d are not found.\n",u,v);
else if (pos[u]==||pos[v]==) printf ("ERROR: %d is not found.\n",pos[u]==?u:v);
else lca (u,v);
}
return ;
}
PAT A1151 LCA in Binary Tree的更多相关文章
- PAT A1151 LCA in a Binary Tree (30 分)——二叉树,最小公共祖先(lca)
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT 甲级 1110 Complete Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...
- PAT 1102 Invert a Binary Tree[比较简单]
1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...
- PAT甲级——1110 Complete Binary Tree (完全二叉树)
此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary ...
- PAT 1102 Invert a Binary Tree
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT甲级——A1110 Complete Binary Tree【25】
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- PAT Advanced 1110 Complete Binary Tree (25) [完全⼆叉树]
题目 Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each ...
- PAT_A1151#LCA in a Binary Tree
Source: PAT A1151 LCA in a Binary Tree (30 分) Description: The lowest common ancestor (LCA) of two n ...
随机推荐
- ACM的探索之Everything Is Generated In Equal Probability(这真的是很有趣的话语丫!)
---------------------------------------步履不停,奋勇前进! ------------------------难度真的是蛮大丫!后序补充!
- 手写webpack配置文件
webpack是一款模块加载器兼打包工具,它能把各种资源,例如JS(含JSX).coffee.样式(含less/sass).图片等都作为模块来使用和处理,它能有Grunt或Gulp所有基本功能. We ...
- 粪发涂墙-redis1
redis 核心就是 如果我的数据全都在内存里,我单线程的去操作 就是效率最高的,为什么呢,因为多线程的本质就是 CPU 模拟出来多个线程的情况,这种模拟出来的情况就有一个代价,就是上下文的切换, 对 ...
- zol验证码抓包
http://www.zol.com/detail/lcd/samsung/25254662.html?zp_from=pro_price_pricenode 网站’ http://city.zol. ...
- Springmvc-crud-06(路径忘记加上“/”错误)
错误: 原因:自己马虎忘记加" / ",罚继续写代码┭┮﹏┭┮ 前端代码: <h1>添加功能</h1> <form action="te ...
- AC自动机讲解超详细
begin:2019/5/2 感谢大家支持! AC自动机详细讲解 AC自动机真是个好东西!之前学KMP被Next指针搞晕了,所以咕了许久都不敢开AC自动机,近期学完之后,发现AC自动机并不是很难,特别 ...
- 解决新版Pycharm中Matplotlib图像不在弹出独立的显示窗口问题
官方说明链接: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000736584-SciView-in-PyCh ...
- Java 通过身份证获取生日和性别
/** * 通过身份证号获取生日和性别 * @param identifyNumber * @return */ private String[] getBirthAndSexByIdNo(Strin ...
- ASP.NET Core中的依赖注入【上】
此为系列文章,对MSDN ASP.NET Core 的官方文档进行系统学习与翻译.其中或许会添加本人对 ASP.NET Core 的浅显理解 ASP.NET Core支持DI软件设计模式,其是一种为了 ...
- DBC物品中打包物品参数设置
DBC库中添加某物品包或捆,主要修改以下这两地方: 物品DBC: Stdmode字段 填写31表示捆或包 Shape字段 表示解开后的物品,填写时需要先在你的服务端文件里面找到UnbindList ...