题意:

给出一棵树的结构。

给出两个点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. 【C++周报】第二期 2021-8-19

    这次我们照样看一道题.个人认为比上一次的简单. https://vijos.org/p/1130 先说方法,动态规划,你能想到什么? "在它的左边加上一个自然数,但该自然数不能超过原数的一半 ...

  2. mysql将数据导入到另外一张操作

    insert into ydcq_member_class (ClassId,signcount,UserId) select 64,2,`员工编号` from `学员名单`

  3. 目标检测之pycocotools安装

    从清华镜像源下载https://pypi.tuna.tsinghua.edu.cn/simple/pycocotools-windows/ wheel型包,pycocotools_windows-2. ...

  4. Python爬虫之PySpider框架

    概述 pyspider 是一个支持任务监控.项目管理.多种数据库,具有 WebUI 的爬虫框架,它采用 Python 语言编写,分布式架构.详细特性如下: 拥有 Web 脚本编辑界面,任务监控器,项目 ...

  5. openTSDB-采集器批量部署-tcollector

        前提: 所需安装采集器服务器与部署服务器之间都已经配置无密码登录 1.安装服务器安装expect包 安装服务器与需要安装Tcollector服务器之间未配置无密登录需要此步. yum inst ...

  6. Java对象构造

    关于对象构造的一些认识. 默认域初始化 如果在构造器中没有显示地给域赋予初值,那么就会被自动地赋予默认值:数值为0,布尔值为false,对象引用为null.然而,这显然是不安全的,在一个null引用上 ...

  7. Jupyter lab 配置记录,xpython + R 语言

    install.packages(c("repr", "IRdisplay", "evaluate", "crayon" ...

  8. Unittest 框架之测试固件-----(setUp与tearDown)你真的会用吗?

    前言 做自动化测试中,需要管理大量的测试用例,如果不用框架管理用例,那会是一件很麻烦的事. 如果所示只写了一个用例,内容就是输入网易邮箱账号和密码 test fixture:测试固件 简单来说就是做一 ...

  9. Jmeter压测学习6---登录参数CSV

    前言 我们在压测登录接口的时候,如果只用一个账号去设置并发压测,这样的结果很显然是不合理的,一个用户并发无法模拟真实的情况.如果要压测登录接口,肯定得准备几百,甚至上千的账号去登录,测试的结果才具有可 ...

  10. IceCream in Python

    IceCream in Python 你还在使用print 在Python 中 debug 吗?赶快使用Icecream吧. 提到 Icecream,你是不是会想到ta? 动机 如果你使用print去 ...