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 ...
随机推荐
- Toad for Oracle 快捷键
F4 看表的结构 F5 执行对话框中的SQL,注意最后需要以;结尾 F7 清除当前编辑框中所有的sql F8 查看历史的sql语句 F9 执行当前行的sql F10 看菜单 Ctrl + F12 保存 ...
- C++向main函数传递参数的方法(实例已上传至github)
通常情况下,我们定义的main函数都只有空形参列表: int main(){...} 然而,有时我们确实需要给mian传递实参,一种常见的情况是用户设置一组选项来确定函数所要执行的操作.例如,假定ma ...
- js动态引入的四种方式
index.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:// ...
- QQ互联OAuth2.0 .NET SDK 发布以及网站QQ登陆示例代码(转)
OAuth: OAuth(开放授权)是一个开放标准,允许用户授权第三方网站访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方网站或分享他们数据的所有内容. QQ登录OAuth2 ...
- 分享:linux下apache服务器的配置和管理
linux下apache服务器的配置和管理. 一.两个重要目录: Apache有两个重要的目录:1.配置目录/etc/httpd/conf:2.文档目录/var/www: 二.两种配置模式: Apac ...
- 如何开启MYSQL远程连接权限
开启MYSQL远程连接权限 //建议设置固定IP mysql> GRANT ALL PRIVILEGES ON *.* TO root@"8.8.8.8" IDENTIFIE ...
- Maven入门指南⑤:使用Nexus搭建Maven私服
1 . 私服简介 私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件.有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库:否则,私服请求外部 ...
- 使用wget备份禅道
禅道7.1,管理了公司所有项目.需要每月备份. 主机安装在一台windows上.为了方便,写个脚本自动调用禅道的备份功能,并把服务器上的备份文件下载到本地. @echo off setlocal re ...
- 使用CSS画一个三角形
<div style="width:0px;height:0px;border-width:40px;border-style:solid;border-color:transpare ...
- openerp 经典收藏 workflow中的‘非典型’自动触发器trigger_model(转载)
workflow中的‘非典型’自动触发器trigger_model 原文:http://cn.openerp.cn/workflow%E4%B8%AD%E7%9A%84%E9%9D%9E%E5%85% ...