POJ 1330 Nearest Common Ancestors(求最近的公共祖先)
题意:给出一棵树,再给出两个节点a、b,求离它们最近的公共祖先。
方法一:
先用vector存储某节点的子节点,fa数组存储某节点的父节点,最后找出fa[root]=0的根节点root。
之后求每个节点的“高度”,更节点的高度为1,每往下一层高度+1。
读取a和b后,先求出它们位于同一个高度的祖先:
1.若此祖先相同,则即为最近的公共祖先。
2.若不相同,则求各自的父节点,知道两者的父节点相同,即为最近的公共祖先。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector> using namespace std;
const int maxn=;
int n;
vector<int> son[maxn];
int depth[maxn]; //depth[i]表示节点i的“高度”,这里根节点高度为1,每往下一层高度+1
int fa[maxn]; //fa[i]表示i的父节点 void init(){
for(int i=;i<maxn;i++)
son[i].clear();
memset(depth,,sizeof(depth));
memset(fa,,sizeof(fa));
}
//求出每个节点的“高度”
void dealdepth(int u){
for(int i=;i<son[u].size();i++){
int v=son[u][i];
depth[v]=depth[u]+;
dealdepth(v);
}
return;
} int solve(int a,int b){
//depth[a]>depth[b],表示b在a的上方,先求出a的与b在同一层的祖先
if(depth[a]>depth[b]){
while(depth[a]>depth[b])
a=fa[a];
if(a==b)
return a;
}
//depth[a]<depth[b],表示a在b的上方,先求出b的与a在同一层的祖先
else if(depth[a]<depth[b]){
while(depth[a]<depth[b])
b=fa[b];
if(a==b)
return a;
}
//同时求祖先,直至相等
while(a!=b){
a=fa[a];
b=fa[b];
}
return a;
}
int main()
{
int t,a,b,root,u,v; //root:根节点
scanf("%d",&t);
while(t--){
init();
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
son[u].push_back(v);
fa[v]=u;
}
for(int i=;i<=n;i++){
if(fa[i]==){
root=i;
break;
}
}
depth[root]=;
dealdepth(root);
scanf("%d%d",&a,&b);
int ans=solve(a,b);
printf("%d\n",ans);
}
return ;
}
方法二:用Tarjan算法求
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <vector>
/*
Tarjan算法
*/
using namespace std;
const int maxn=;
int n,t,a,b; //a,b是要求公共祖先的两点
bool flag; //flag为true表明LCA已经求出了a,b的公共祖先,那么就不用继续往下求了
int vis[maxn]; //LCA标记哪些节点已被访问过
int indegree[maxn]; //记录每个节点的入度,用来找寻根节点
int head[maxn];
int tot;
int ans; struct Edge{
int to,next;
}edge[maxn]; void add(int u,int v){
edge[tot].next=head[u];
edge[tot].to=v;
head[u]=tot++;
}
//并查集
struct UF{
int fa[maxn];
void init(){
for(int i=;i<=n;i++)
fa[i]=i;
}
int find_root(int x){
if(fa[x]!=x)
fa[x]=find_root(fa[x]);
return fa[x];
}
void Union(int u,int v){
fa[v]=fa[u];
}
}uf; void LCA(int u){
if(flag)
return;
int v;
for(int k=head[u];k!=-;k=edge[k].next){
v=edge[k].to;
LCA(v);
uf.Union(u,v);
}
vis[u]=; //标记节点u
//看看u是否是查询的两个节点中的一个
if(u==a){
if(vis[b]){
ans=uf.fa[uf.find_root(b)];
flag=true;
}
}
else if(u==b){
if(vis[a]){
ans=uf.fa[uf.find_root(a)];
flag=true;
}
}
}
int main()
{
int root,u,v;
cin>>t;
while(t--){
memset(indegree,,sizeof(indegree));
memset(head,-,sizeof(head));
tot=;
cin>>n;
for(int i=;i<n;i++){
cin>>u>>v;
add(u,v);
indegree[v]++;
}
for(int i=;i<=n;i++){
if(!indegree[i]){
root=i;
break;
}
}
cin>>a>>b;
flag=false;
memset(vis,,sizeof(vis));
uf.init();//一开始忘记初始化了额
LCA(root);
cout<<ans<<endl; }
return ;
}
POJ 1330 Nearest Common Ancestors(求最近的公共祖先)的更多相关文章
- poj 1330 Nearest Common Ancestors 求最近祖先节点
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37386 Accept ...
- POJ 1330 Nearest Common Ancestors 倍增算法的LCA
POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- POJ.1330 Nearest Common Ancestors (LCA 倍增)
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- LCA POJ 1330 Nearest Common Ancestors
POJ 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24209 ...
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
- POJ 1330 Nearest Common Ancestors 【LCA模板题】
任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000 ...
- POJ 1330 Nearest Common Ancestors(Targin求LCA)
传送门 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26612 Ac ...
随机推荐
- ADO.NET笔记——存储二进制大对象(BLOB)
相关知识 上传二进制大对象(Binary Large Object)(如图片.视频等)的基本编程步骤是: 在数据库中使用varbinary(MAX).varchar(MAX)或者nvarchar(MA ...
- Android混淆问题
最近做了2个项目,全部要混淆,刚接触,自己在网上找了还多资料,感觉各有千秋,自己总结了一下,第一次发帖,不喜勿喷.求各种指导!!! android应用程序的混淆打包规范 1.在工程文件project. ...
- mongodb的常用操作
对于nosql之前工作中有用到bekerlydb,最近开始了解mongodb,先简单写下mongodb的一些常用操作,当是个总结: 1.mongodb使用数据库(database)和集合(collec ...
- [大牛翻译系列]Hadoop(19)MapReduce 文件处理:基于压缩的高效存储(二)
5.2 基于压缩的高效存储(续) (仅包括技术27) 技术27 在MapReduce,Hive和Pig中使用可分块的LZOP 如果一个文本文件即使经过压缩后仍然比HDFS的块的大小要大,就需要考虑选择 ...
- Hadoop上路-01_Hadoop2.3.0的分布式集群搭建
一.配置虚拟机软件 下载地址:https://www.virtualbox.org/wiki/downloads 1.虚拟机软件设定 1)进入全集设定 2)常规设定 2.Linux安装配置 1)名称类 ...
- asp.net图片上传实例
网站后台都需要有上传图片的功能,下面的例子就是实现有关图片上传. 缺点:图片上传到本服务器上,不适合大量图片上传. 第一.图片上传,代码如下: xxx.aspx 复制代码代码如下: <td cl ...
- setEllipsize(TruncateAt where)
void android.widget.TextView.setEllipsize(TruncateAt where) public void setEllipsize (TextUtils.Trun ...
- 内部技术分享的 PPT
本文的基础是搞了一次内部的技术分享,在此也分享一下本次的PPT的一些内容.先列一下大概内容吧. EF-Code First API(WCF.WebAPI) Xaml MVVM AOP Xamarin. ...
- cadence通过孔焊盘的制作
1 首先制作flash 1)制作焊盘前先计算好各项数据 thermal relief(热风焊盘):内径(ID)= 孔径 +20mil 外径(OD)= Anti_pad的直径= Regular p ...
- GITHUB 提交错误 Error: Permission denied (publickey) 解决
1. 在开发机上生成自己的密钥 ssh-keygen -b 1024 -t rsa -b 指密钥对长度 -t 指加密方式 Enter file in which to save the key ( ...