题意:

给出一棵树的结构。

给出两个点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. 3.8学习总结——Android保存信息

    为了保存软件的设置参数,Android平台为我们提供了一个SharedPreferences接口,它是一个轻量级的存储类,特别适合用于保存软件配置参数.使用SharedPreferences保存数据, ...

  2. python学习笔记(十一)-python程序目录工程化

    在一个程序当中,一般都会包含文件夹:bin.conf.lib.data.logs,以及readme文件. 所写程序存放到各自的文件夹中,如何进行串联? 首先,通过导入文件导入模块方式,引用其他人写好的 ...

  3. 『GoLang』数组与切片

    数组 数组是具有相同唯一类型的一组已编号且长度固定的数据项序列(这是一种同构的数据结构):这种类型可以是任意的原始类型例如整型.字符串或者自定义类型. 数组长度必须是一个常量表达式,并且必须是一个非负 ...

  4. JPA基本用法

    jpa基本查询 1.继承JpaRepository,生成了CRUD方法 public void testBaseQuery() throws Exception {   User user=new U ...

  5. HttpClient遭遇Connection Reset异常,如何正确配置?

    最近工作中使用的HttpClient工具遇到的Connection Reset异常.在客户端和服务端配置不对的时候容易出现问题,下面就是记录一下如何解决这个问题的过程. 出现Connection Re ...

  6. SpringBoot如何实现定时任务

    写在前面 SpringBoot创建定时任务的方式很简单,主要有两种方式:一.基于注解的方式(@Scheduled)二.数据库动态配置.实际开发中,第一种需要在代码中写死表达式,如果修改起来,又得重启会 ...

  7. 洛谷2046 NOI2010海拔

    QwQ题目太长 这里就不复制了 题目 这个题...算是个比较经典的平面图最小割变成对偶图的最短路了QwQ 首先考虑最小割应该怎么做. 有一个性质,就是每个点的海拔要么是1,要么是0 QwQ不过这个我不 ...

  8. 数据库已经存在表, django使用inspectdb反向生成model实体类

    1.通过inspectdb处理类,可以将现有数据库里的一个或者多个.全部数据库表生成Django model实体类 python manage.py inspectdb --database defa ...

  9. final和static的区别

    static作用于成员变量用来表示只保存一份副本 final的作用是用来保证变量不可变.下面代码验证一下 public class FinalTest { public static void mai ...

  10. windows右键菜单自动打包发布nuget,没有CI/CD一样方便!

    构建现代的 .Net 应用离不开 Nuget 的支持,而快速打包 Nuget 成了提高生产率的有效方法.没有CI/CD?来试试使用windows右键菜单吧 先看右键效果图 有时候我们可能没有CI/CD ...