1143 Lowest Common Ancestor(建树与不建两种思路)
目录
解法一
解法二
解法一
这题可以不建树,直接利用BST的性质:左子树<根节点<右子树,对先序序列进行遍历,如果有某个元素大于等于u,v中较小的且小于等于u,v中较大的,则可能是根节点。
这题数据弱,直接认为这样的点是根节点也AC了。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>
#include<stdlib.h>
#include<time.h>
#include<vector>
#include<set>
#include<string>using namespace std;
typedef long long LL;const int maxn = 10007;
const int MOD = 1000000007;
const int INF = 1000000000;//INF:下确界
const LL SUP = (1LL<<63)-1;//SUP:上确界
const double eps = 1e-5;int A[maxn];
int n;map<int,int> eximp;int main(){int pNum;cin>>pNum>>n;for(int i=0;i<n;i++){cin>>A[i];eximp[A[i]] = 1;}while(pNum--){int v1,v2;cin>>v1>>v2;if(eximp[v1]==0||eximp[v2]==0){if(eximp[v1]==1)printf("ERROR: %d is not found.\n",v2);else if(eximp[v2]==1)printf("ERROR: %d is not found.\n",v1);else printf("ERROR: %d and %d are not found.\n",v1,v2);continue;}int LCA;for(int i=0;i<n;i++){LCA = A[i];if(LCA>=min(v1,v2)&&LCA<=max(v1,v2))break;}if(LCA==v1||LCA==v2)printf("%d is an ancestor of %d.\n",LCA==v1?v1:v2,LCA==v1?v2:v1);else printf("LCA of %d and %d is %d.\n",v1,v2,LCA);}return 0;
}
解法二
我尝试建树,然后用缩短系谱的通法找LCA,但是超时了。
代码
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>
#include<stdlib.h>
#include<time.h>
#include<vector>
#include<set>
#include<string>using namespace std;
typedef long long LL;const int maxn = 10007;
const int MOD = 1000000007;
const int INF = 1000000000;//INF:下确界
const LL SUP = (1LL<<63)-1;//SUP:上确界
const double eps = 1e-5;int A[maxn];
int n;map<int,int> eximp;struct Node{int v;Node *lchild,*rchild,*pa;
};map<Node*,int> noToV;
map<int,Node*> vToNo;void insert(int x,Node* &root){if(root==NULL){root = new Node;noToV[root] = x;vToNo[x] = root;root->v = x;root->lchild = root->rchild = root->pa = NULL;return; }if(root->v>x){insert(x,root->lchild);if(!root->lchild->pa)root->lchild->pa = root;}if(root->v<x){insert(x,root->rchild);if(!root->rchild->pa)root->rchild->pa = root;}}int getLinkLen(Node* root){int len = 1;while(root->pa){root = root->pa;len ++;}return len;
}Node* getLCA(Node* u,Node* v){int uLen = getLinkLen(u);int vLen = getLinkLen(v);while(uLen<vLen){v = v->pa;vLen --;}while(vLen<uLen){u = u->pa;uLen --;}while(v!=u){v = v->pa;u = u->pa;}return v;
}int main(){int pNum;cin>>pNum>>n;for(int i=0;i<n;i++){cin>>A[i];eximp[A[i]] = 1;}Node* root = NULL;for(int i=0;i<n;i++){insert(A[i],root);}while(pNum--){int v1,v2;cin>>v1>>v2;if(eximp[v1]==0||eximp[v2]==0){if(eximp[v1]==1)printf("ERROR: %d is not found.\n",v2);else if(eximp[v2]==1)printf("ERROR: %d is not found.\n",v1);else printf("ERROR: %d and %d are not found.\n",v1,v2);continue;}int LCA;Node* u = vToNo[v1];Node* v = vToNo[v2];LCA = noToV[getLCA(u,v)];if(LCA==v1||LCA==v2)printf("%d is an ancestor of %d.\n",LCA==v1?v1:v2,LCA==v1?v2:v1);else printf("LCA of %d and %d is %d.\n",v1,v2,LCA);}return 0;
}
结果
1143 Lowest Common Ancestor(建树与不建两种思路)相关推荐
- PAT甲级1143 Lowest Common Ancestor (30 分):[C++题解]LCA、最低公共祖先
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:二叉搜索树的中序遍历是隐含给定的,它的中序遍历就是从小到大排列. 所以这道题先是根据给定的前序遍历和中序遍历,建树. 建树的时候需要用 ...
- 【题解】【PAT甲】1143 Lowest Common Ancestor (30 分)(树)(BST)(最近公共祖先)
题目链接 PTA | 程序设计类实验辅助教学平台 题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the ...
- 1143. Lowest Common Ancestor
1143. Lowest Common Ancestor (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 1143 Lowest Common Ancestor 甲级
题意: 给出一棵二叉搜索树的前序遍历,问结点u和v的共同最低祖先是谁,利用先序遍历特点. 二叉搜索树满足: 节点的左子树只包含键小于节点键的节点. 节点的键只包含节点的右键大于或等于子树的节点的键. ...
- 1143 Lowest Common Ancestor (30 分)【难度: 中 / 知识点: 最低公共祖先 未完成】
https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312
- PAT A1143 Lowest Common Ancestor ——沉舟侧畔千帆过,病树前头万木春
PAT A1143 Lowest Common Ancestor 第一次遇到LCA,想的比较乱,感觉有点并查集的意思,又好像不行.开始的想法是用BST的性质和前序建树,建树过程中做一个father数组 ...
- Lowest Common Ancestor of a Binary Search Tree(树中两个结点的最低公共祖先)
题目描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...
- 235 Lowest Common Ancestor of a Binary Search Tree
题目 235 Lowest Common Ancestor of a Binary Search Tree 因为是binary search tree,因此利用没个节点的值进行二分查找即可复杂度O(h ...
- C语言二叉树的lowest common ancestor最低公共祖先(附完整源码)
C语言二叉树的lowest common ancestor最低公共祖先 C语言二叉树的lowest common ancestor最低公共祖先完整源码(定义,实现,main函数测试) C语言二叉树的l ...
最新文章
- python批量删缩进_Python工具PyCharm常用快捷键
- UE3 预计算可见性
- 真不一样了!苹果iPhone 14外形提前曝光:采用“打孔+药丸”的设计
- Maven 动态Web的创建 及 Tomcat的启动
- 马库斯:DeepMind新出的机器心智网络不错,但有误导性
- Git 基础(八)—— 分支管理
- Tensor flow 实战Google深度学习框架 笔记摘要Pthree(二)
- 【没有assembly目录】spark2.0.0启动时无法访问spark-assembly-*.jar的解决办法
- 【LOJ】#2187. 「SHOI2014」三叉神经树
- Google Python 编程风格指南
- android imagebutton 设置边框,Android ImageButton没有边框但仍然有点击指示
- 基于Android的家校联系平台开发(论文)
- 自动控制原理知识点整合归纳(韩敏版)
- 升级ubuntu 16.04的新内核时出现依赖libssl1.1.0问题
- 调整亮度、对比度、饱和度和色相
- 完整简洁的Oracle获得汉字字符串拼音首字母和全拼的函数
- C语言程序设计作业04
- 企业微信hook接口,协议开发,群操作功能教程
- 如何降低研究生硕士论文的查重率
- 163邮箱自动化登录实现模块化【2】