题意:

给出一棵树的结构。

给出两个点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. PTA 面向对象程序设计6-2 统计数字

    对于给定的一个字符串,统计其中数字字符出现的次数. 类和函数接口定义: 设计一个类Solution,其中包含一个成员函数count_digits,其功能是统计传入的string类型参数中数字字符的个数 ...

  2. VSCode Remote-SSH 连接服务器

  3. mysql5.7执行sql语句提示Expression #1 of ORDER BY clause is not in GROUP BY

    mysql 新版本出现group by 语句不兼容问题 [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause ...

  4. P1074 [NOIP2009 提高组] 靶形数独

    #include<bits/stdc++.h> using namespace std; const int N=10; int a[N][N],ans[N][N],vis[3][N][N ...

  5. python BeautifulSoup html解析

    * BeautifulSoup 的.find(), .findAll() 函数原型 findAll(tag, attributes, recursive, text, limit, keywords) ...

  6. JMeter脚本开发

    什么是jmeter脚本 用户操作系统的动作流程 用户操作系统的请求 类似演戏的剧本 怎么快速开发漂亮的jmeter脚本 准确 快速 漂亮,脚本逻辑清晰,维护性高 脚本开发方案 代理 http代理服务器 ...

  7. 网站优化之mysql优化

    一,网站优化之mysql优化:1.前缀索引,可以通过前缀去识别唯一性,把这个前缀作为索引内容,可以节省存储索引的空间,从而提高索引的查询速度.distinct 排重操作2,2.in条件索引使用同时查询 ...

  8. setTimeout 与setInterval的区别

    setTimeout(code,millisec) 方法用于在指定的毫秒数后调用函数或计算表达式 setInterval(code,millisec) 方法可按照指定的周期(以毫秒计)来调用函数或计算 ...

  9. 《使用Jmeter进行批量发送http请求》

    本文主要针对批量接口发送数据 一:接口测试的环境准备 1:JDK的安装:网上下载即可>1.6.0版本以上 2:jemeter工具的下载 (免安装):网上下载即可 3:插件的下载安装地址:http ...

  10. [转载]CentOS 7 创建本地YUM源

    本文中的"本地YUM源"包括三种类型:一是直接使用CentOS光盘作为本地yum源,优点是简单便捷,缺点是光盘软件包可能不完整(centos 7 Everything 总共才6.5 ...