poj 1330 Nearest Common Ancestors (最简单的LCA)
题意:
给出一棵树的结构。
给出两个点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)的更多相关文章
- 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(Targin求LCA)
传送门 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26612 Ac ...
- POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)
LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...
- POJ 1330 Nearest Common Ancestors (模板题)【LCA】
<题目链接> 题目大意: 给出一棵树,问任意两个点的最近公共祖先的编号. 解题分析:LCA模板题,下面用的是树上倍增求解. #include <iostream> #inclu ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- 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节 ...
- 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 ...
随机推荐
- 彻底搞明白PHP的中引用的概念
之前我们其实已经有过几篇文章讲过引用方面的问题,这次我们来全面的梳理一下引用在PHP到底是怎么回事,它和C中的指针有什么不同,在使用的时候要注意些什么. 什么是引用? 在 PHP 中引用意味着用不同的 ...
- 一起搞懂PHP的错误和异常(二)
上回文章中我们讲到了错误是编译和语法运行时会出现的,它们与逻辑无关,是程序员在码代码时不应该出现的,也就是说,这些错误应该是尽量避免带到线上环境的,他们不能通过try...catch捕获到.而异常则正 ...
- Java基础系列(27)- 什么是方法
何谓方法 System.out.println();它是什么呢 # System:类 # out:对象 # println():方法 Java方法是语句的集合,它们在一起执行一个功能 方法是解决一类问 ...
- Java基础系列(19)- Switch结构
package struct; public class SwitchDemo01 { //case穿透 //switch 匹配一个具体的值 public static void main(Strin ...
- csv或excel的utf-8乱码问题
方法1.数据导入 打开 Excel,执行"数据"->"自文本",选择 CSV 文件,出现文本导入向导,选择"分隔符号",下一步,勾选& ...
- js 命令模式 组合模式
* 基本宏命令 var closeDoorCommand = { execute: function() { console.log("Closing the door..."); ...
- Jenkins无法登陆解决方案
Jenkins-2.204.1 版本 创建jenkins用户时,没填full name,且选择了使用系统的admin登录或者是admin登录只是改了admin的登录密码导致登录不上去(Invalid ...
- django 内置用户-装饰器
""" 一.如何给python内置用户添加额外的字段,注意一定义在没有迁移数据之前定义,否则会报错 1.在models中先调用 from django.contrib.a ...
- 鸿蒙源码分析系列(总目录) | 百万汉字注解 百篇博客分析 | 深入挖透OpenHarmony源码 | v8.23
百篇博客系列篇.本篇为: v08.xx 鸿蒙内核源码分析(总目录) | 百万汉字注解 百篇博客分析 | 51.c.h .o 百篇博客.往期回顾 在给OpenHarmony内核源码加注过程中,整理出以下 ...
- scheduler源码分析——preempt抢占
前言 之前探讨scheduler的调度流程时,提及过preempt抢占机制,它发生在预选调度失败的时候,当时由于篇幅限制就没有展开细说. 回顾一下抢占流程的主要逻辑在DefaultPreemption ...