题意:

给出一棵树的结构。

给出两个点X和Y,求它俩的LCA。

思路:

只需求两个点的LCA,用了两种方法,一种离线tarjan,一种直接搞。

看代码。

代码:

方法一:直接搞。

int const maxn = 10005;

int T,n,a,b;
int fa[maxn];
int X,Y; int main(){
cin>>T;
while(T--){
scanf("%d",&n);
mem(fa,-1);
rep(i,1,n-1){
scanf("%d%d",&a,&b);
fa[b]=a;
}
scanf("%d%d",&X,&Y);
map<int,char> mp;
int t1=X;
while(t1!=-1){
mp[t1]=1;
t1=fa[t1];
}
t1=Y;
while(t1!=-1){
if(mp[t1]==1){
printf("%d\n",t1);
break;
}
t1=fa[t1];
}
}
}

方法二:离线tarjan。

int const maxn = 10005;

struct node{
int to,w,next,lca;
};
node edge[maxn*2]; bool vis[maxn];
int cnt1,cnt2;
int head[maxn], fa[maxn];
bool flag;
int X,Y,n; inline void Addedge(int u,int v,int w){
edge[++cnt1].w=w;
edge[cnt1].to=v;
edge[cnt1].next=head[u];
head[u]=cnt1;
} void init(){
mem(head,-1);
mem(vis,false);
cnt1=0;
flag=false;
rep(i,1,n) fa[i]=i;
} int findFa(int x){
if(fa[x]==x) return fa[x];
return fa[x]=findFa(fa[x]);
}
void Tarjan_LCA(int u){ //离线LCA算法
if(flag)
return;
fa[u]=u;
vis[u]=true;
for(int i=head[u];i!=-1;i=edge[i].next){
if(!vis[edge[i].to]){
Tarjan_LCA(edge[i].to);
fa[edge[i].to]=u;
}
}
if(u==Y && !flag){
if(vis[X]){
printf("%d\n",findFa(X));
flag=true;
return;
}
}
if(u==X && !flag){
if(vis[Y]){
printf("%d\n",findFa(Y));
flag=true;
return;
}
}
} int T,a,b; int main(){
cin>>T;
while(T--){
scanf("%d",&n);
init();
bool t1[maxn]={0};
rep(i,1,n-1){
scanf("%d%d",&a,&b);
Addedge(a,b,1);
t1[b]=true;
}
scanf("%d%d",&X,&Y);
rep(i,1,n) if(!t1[i]){
Tarjan_LCA(i); //i为树的根结点
break;
}
}
}

poj 1330 Nearest Common Ancestors (最简单的LCA)的更多相关文章

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

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

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

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

  3. POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)

    LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...

  4. POJ 1330 Nearest Common Ancestors (模板题)【LCA】

    <题目链接> 题目大意: 给出一棵树,问任意两个点的最近公共祖先的编号. 解题分析:LCA模板题,下面用的是树上倍增求解. #include <iostream> #inclu ...

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

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

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

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

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

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

  8. LCA POJ 1330 Nearest Common Ancestors

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

  9. POJ 1330 Nearest Common Ancestors(lca)

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

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

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

随机推荐

  1. 学习PHP中的国际化日期格式化操作

    对于国际化功能来说,日期相关的格式化操作也是一块重头戏,毕竟不同的时区,不同的国家对于日期的表示方式都会有些不同.今天我们主要来学习的就是国际化地表示日期相关的信息内容. 日期格式化 首先就是最直接的 ...

  2. [手机编程]Aid Learning--换源+数据库安装

    换源+MYSQL安装 Aid Learning下载安装 http://www.aidlearning.net/ 切换源 打开Terminal复制回车即可 cd /etc/apt/&& ...

  3. P4091-[HEOI2016/TJOI2016]求和【斯特林数,NTT】

    正题 题目链接:https://www.luogu.com.cn/problem/P4091 题目大意 给出\(n\),求 \[\sum_{i=0}^n\sum_{j=0}^i\begin{Bmatr ...

  4. 自学 Python,视频教程和代码一看就懂,动手就废,应该这么学

    ​ 一.代码量太少了,看得多做得少,导致一做就错. 每一个测试工程师必定是在大量的时间和代码中提升的自己,如果你只是看视频的话,那永远都停留在理论上,很多问题是要实践才能发现的 我打个比方你看视频的时 ...

  5. Linux命令行:free

                  total        used        free      shared  buff/cache   availableMem:           251G   ...

  6. .NET 排序 Array.Sort<T> 实现分析

    System.Array.Sort<T> 是.NET内置的排序方法, 灵活且高效, 大家都学过一些排序算法,比如冒泡排序,插入排序,堆排序等,不过你知道这个方法背后使用了什么排序算法吗? ...

  7. 模仿ToDoList

    1.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  8. 微信小程序应用安全分析及设计

    针对微信关于小程序安全设计的分析 针对微信小程序开发配置及部分配置机制分析微信小程序安全设计: AppSecret 管理员生成AppSecret,在与微信后台交互过程中部分接口使用,如 auth.co ...

  9. 山顶点提取(ArcPy实现)

    一.背景 山顶点指哪些在特定邻域分析范围内,该点都比周围点高的区域.山顶点是地形的重要特征点,它的分布与密度反映了地貌的发育特征,同时也制约着地貌发育.因此,如何基于DEM数据正确有效的提取山顶点,在 ...

  10. 使用Mybatis的一些基本配置及Mybatis与数据库交互测试验证

    1.简介 什么是MyBatis? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.My ...