题意:给出一棵树,再给出两个节点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(求最近的公共祖先)的更多相关文章

  1. poj 1330 Nearest Common Ancestors 求最近祖先节点

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37386   Accept ...

  2. POJ 1330 Nearest Common Ancestors 倍增算法的LCA

    POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...

  3. POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

  4. POJ.1330 Nearest Common Ancestors (LCA 倍增)

    POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...

  5. POJ - 1330 Nearest Common Ancestors(基础LCA)

    POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %l ...

  6. LCA POJ 1330 Nearest Common Ancestors

    POJ 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24209 ...

  7. POJ 1330 Nearest Common Ancestors(lca)

    POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...

  8. POJ 1330 Nearest Common Ancestors 【LCA模板题】

    任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000 ...

  9. POJ 1330 Nearest Common Ancestors(Targin求LCA)

    传送门 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26612   Ac ...

随机推荐

  1. VS2010使用TeeChart5的ColorGrid绘制一维距离像

    绘制一维距离像原理:使用TeeChart控件中的ColorGrid显示(X,Y,Z)三维数据,X和Z分别代表一维距离像的x轴和y轴数据,Y代表对应的数值,以不同颜色显示. 1.注册TeeChart5 ...

  2. SP避免Form重复提交的三种方案

    SP避免Form重复提交的三种方案  1) javascript ,设置一个变量,只允许提交一次.   <script language="javascript">  ...

  3. 【风马一族_Android】Android 前端内容

    Android 前端内容 4.1 View 类概述 4.1.1 关于 View //类型说明 view(视图)指的是用户界面组件的基本构建基块.一个视图占据屏幕上的矩形区域,负责绘图和事件处理.视图是 ...

  4. 数列平方根的和 java

    题目描述: 数列的定义如下:数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和. 输入 输入数据有多组,每组占一行,由两个整数n(n<10000)和m(m<1000)组成,n和 ...

  5. 启语外语培训网SEO历程

    网站开发完成已经好长时间了,但收录一直上不去,排名也上不去.我这篇文章主要是分析一下网站的SEO历程 网址:http://waiyupx.cheer-edu.net/School/School-5.h ...

  6. AJAX 跨域 :Access-Control-Allow-Origin

    在一个项目上想用NodeJS,在前端的JS(http://localhost/xxx)中ajax访问后端RestAPI(http://localhost:3000/….)时(Chrome)报错: XM ...

  7. 【Qt】Qt之自定义界面(窗体缩放)【转】

    简述 通过前两节内容,我们实现了自定义窗体的移动,以及自定义标题栏-用来显示窗体的图标.标题,以及控制窗体最小化.最大化.关闭. 在这之后,我们还缺少窗体的缩放-当鼠标移动到窗体的边框-左.上.右.下 ...

  8. 安装gitolite,并ssh公钥无密码登录

    安装gitolite,并ssh公钥无密码登录 gitolite是管理git版本库的一种方案,它将git版本库的管理信息放在了一个特殊git版本库里.gitolite与linux操作系统集成了,需要使用 ...

  9. @RenderSection与@RenderBody

    _LayoutMain: <html> <head> @RenderSection("head") </head> <body> @ ...

  10. MinGW-64 安装

    一.在mingw-w64官网下载mingw-w64在线安装包 二.点击mingw-w64进行安装,选择: Version:选最新版本 我这个是4.9.2 Architecture:x86_64 (64 ...