1、输入树中的节点数N,输入树中的N-1条边。最后输入2个点,输出它们的最近公共祖先。

2、裸的最近公共祖先。

3、

dfs+ST在线算法:

/*
LCA(POJ 1330)
在线算法 DFS+ST
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; const int MAXN=;
int rmq[*MAXN];//rmq数组,就是欧拉序列对应的深度序列
struct ST{
int mm[*MAXN];
int dp[*MAXN][];//最小值对应的下标
void init(int n){
mm[]=-;
for(int i=;i<=n;i++){
mm[i]=((i&(i-))==)?mm[i-]+:mm[i-];
dp[i][]=i;
}
for(int j=;j<=mm[n];j++)
for(int i=;i+(<<j)-<=n;i++)
dp[i][j]=rmq[dp[i][j-]]<rmq[dp[i+(<<(j-))][j-]]?dp[i][j-]:dp[i+(<<(j-))][j-];
}
//查询[a,b]之间最小值的下标
int query(int a,int b){
if(a>b)swap(a,b);
int k=mm[b-a+];
return rmq[dp[a][k]]<=rmq[dp[b-(<<k)+][k]]?dp[a][k]:dp[b-(<<k)+][k];
}
};
//边的结构体定义
struct Edge{
int to,next;
};
Edge edge[MAXN*];
int tot,head[MAXN]; int F[MAXN*];//欧拉序列,就是dfs遍历的顺序,长度为2*n-1,下标从1开始
int P[MAXN];//P[i]表示点i在F中第一次出现的位置
int cnt;
ST st;
void init(){
tot=;
memset(head,-,sizeof(head));
}
//加边,无向边需要加两次
void addedge(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void dfs(int u,int pre,int dep){
F[++cnt]=u;
rmq[cnt]=dep;
P[u]=cnt;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==pre)continue;
dfs(v,u,dep+);
F[++cnt]=u;
rmq[cnt]=dep;
}
}
//查询LCA前的初始化
void LCA_init(int root,int node_num){
cnt=;
dfs(root,root,);
st.init(*node_num-);
}
//查询u,v的lca编号
int query_lca(int u,int v){
return F[st.query(P[u],P[v])];
}
bool flag[MAXN];
int main(){
int T;
int N;
int u,v;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
init();
memset(flag,false,sizeof(flag));
for(int i=;i<N;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
flag[v]=true;
}
int root;
for(int i=;i<=N;i++)
if(!flag[i]){
root=i;
break;
}
LCA_init(root,N);
scanf("%d%d",&u,&v);
printf("%d\n",query_lca(u,v));
}
return ;
}

LCA倍增法:

/*
POJ 1330
LCA在线算法
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; const int MAXN=;
const int DEG=; struct Edge{
int to,next;
}edge[MAXN*];
int head[MAXN],tot;
void addedge(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void init(){
tot=;
memset(head,-,sizeof(head));
}
int fa[MAXN][DEG];//fa[i][j]表示结点i的第2^j个祖先
int deg[MAXN];//深度数组 void BFS(int root){
queue<int>que;
deg[root]=;
fa[root][]=root;
que.push(root);
while(!que.empty()){
int tmp=que.front();
que.pop();
for(int i=;i<DEG;i++)
fa[tmp][i]=fa[fa[tmp][i-]][i-];
for(int i=head[tmp];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==fa[tmp][])continue;
deg[v]=deg[tmp]+;
fa[v][]=tmp;
que.push(v);
}
}
}
int LCA(int u,int v){
if(deg[u]>deg[v])swap(u,v);
int hu=deg[u],hv=deg[v];
int tu=u,tv=v;
for(int det=hv-hu,i=;det;det>>=,i++)
if(det&)
tv=fa[tv][i];
if(tu==tv)return tu;
for(int i=DEG-;i>=;i--){
if(fa[tu][i]==fa[tv][i])
continue;
tu=fa[tu][i];
tv=fa[tv][i];
}
return fa[tu][];
}
bool flag[MAXN];
int main(){
int T;
int n;
int u,v;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
init();
memset(flag,false,sizeof(flag));
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
flag[v]=true;
}
int root;
for(int i=;i<=n;i++)
if(!flag[i]){
root=i;
break;
}
BFS(root);
scanf("%d%d",&u,&v);
printf("%d\n",LCA(u,v));
}
return ;
}

POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)的更多相关文章

  1. POJ 1330 Nearest Common Ancestors 【最近公共祖先LCA算法+Tarjan离线算法】

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20715   Accept ...

  2. poj 1330 Nearest Common Ancestors (最简单的LCA)

    题意: 给出一棵树的结构. 给出两个点X和Y,求它俩的LCA. 思路: 只需求两个点的LCA,用了两种方法,一种离线tarjan,一种直接搞. 看代码. 代码: 方法一:直接搞. int const ...

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

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

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

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

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

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

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

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

  7. LCA POJ 1330 Nearest Common Ancestors

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

  8. POJ 1330 Nearest Common Ancestors(lca)

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

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

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

随机推荐

  1. 624. Maximum Distance in Arrays

    Problem statement Given m arrays, and each array is sorted in ascending order. Now you can pick up t ...

  2. Uva10294 Arif in Dhaka (置换问题)

    扯回正题,此题需要知道的是置换群的概念,这点在刘汝佳的书中写的比较详细,此处不多做赘述.此处多说一句的是第二种手镯的情况.在下图中“左图顺时针转1个位置”和“右图顺时针旋转5个位置”是相同的,所以在最 ...

  3. HDU3430 (置换群循环节+中国剩余定理)

    题意:给出n张牌,标号为1-n,然后给出两个序列,序列1表示序列1,2,3,4……,n洗一次牌后到达的,序列2表示目标序列,问初始序列按序列1的洗牌方式洗几次能到达序列2的情况,如果不能到达输出-1. ...

  4. topcoder 650 srm

    500 遇到这种构造题 就给跪了 比赛的时候想很多方法 DP,贪心,模拟 发现越写越烦琐.看到别人出这么快,肯定又是奇葩思路. 后来居然想到 2^50的暴力 +剪枝 不过暴力肯定卡你 IDEA: 只要 ...

  5. HUST 1328 String

    11: KMP next 的强大 题意求前缀在S中出现的次数之和 next[j] 表示 S[0....NEXT[J]]==S[J-NEXT[J].....J]; 于是我们得到..后加入一个字符所得到新 ...

  6. nexus-3本地下载jar的settipng.xml配置

    打开maven安装目录下的setting.xml <servers> <server> <id>nexus</id> <username>a ...

  7. HOST绑定和VIP映射

    今天上线需要配置RAL,处理半天,发现是需要HOST和IP分开来配. 比如: curl -H "Host: ktvin.nuomi.com" "http://10.207 ...

  8. 【网络】TCP的拥塞控制

    一.拥塞控制的一般原理 拥塞:对网络中某一资源的需求超过了该资源所能提供的可用部分 拥塞控制是防止过多的数据注入到网络,这样可以使网络中的路由器或链路不致过载,拥塞控制是一个全局性的过程. 流量控制往 ...

  9. [转]JAVA异常

    异常 异常就是导致程序中断执行的一段指令流. 在java中, 对于异常在API中也有明确的定义,叫做异常类. Error : JVM的错误, 程序中不进行处理, 交给虚拟机. Exception : ...

  10. [Bash] View Files and Folders in Bash

    Sometimes when working at the command line, it can be handy to view a file’s contents right in the t ...