题目请戳这里

题目大意:意如其名。

题目分析:本题只有一个查询,所以可以各种乱搞过去。

不过对于菜鸟而言,还是老老实实练习一下LCA算法。

LCA有很多经典的算法。按工作方式分在线和离线2种。

tarjan算法是经典的离线算法。这篇博客讲的太好懂了,我也不好意思班门弄斧,具体戳进去看看就会明白。重点是那个插图,一看秒懂。

在线算法主要有倍增算法和转RMQ算法。

另外LCA还有2种更为高效的O(n)-O(1)算法。一种请戳这里,另一种其实就是先将LCA转化成RMQ,再利用笛卡尔树O(n)预处理,O(1)回答,具体可以戳这里

后两种O(n)算法还没有仔细研究,大致看了下,不是很明白,但是感觉很厉害的样子。mark一下,以后抽时间学习一下。

下面给出本题的前3种算法具体实现:

1:tarjan算法(虽然对本题来说有点奢侈了。。)

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 10005;
struct node
{
int to,next;
}e[N];
int head[N],set[N],fa[N],in[N];
bool vis[N];
int n,num,p,q;
void build(int s,int ed)
{
e[num].to = ed;
e[num].next = head[s];
head[s] = num ++;
}
void init()
{
num = 0;
memset(head,-1,sizeof(head));
memset(in,0,sizeof(in));
}
int find(int x)
{
int rt = x;
while(set[rt] != rt)
rt = set[rt];
int pa = set[x];
while(pa != rt)
{
set[x] = rt;
x = pa;
pa = set[x];
}
return rt;
}
void bing(int a,int b)
{
int ra = find(a);
int rb = find(b);
if(ra != rb)
set[rb] = ra;
}
void dfs(int cur)
{
fa[cur] = cur;
set[cur] = cur;
int i;
for(i = head[cur];i != -1;i = e[i].next)
{
dfs(e[i].to);
bing(cur,e[i].to);
fa[find(cur)] = cur;
}
vis[cur] = true;
if((p == cur && vis[q]))
printf("%d\n",fa[find(q)]);
if((q == cur && vis[p]))
printf("%d\n",fa[find(p)]);
}
void tarjan()
{
int i;
memset(vis,false,sizeof(vis));
for(i = 1;i <= n;i ++)
if(in[i] == 0)
break;
dfs(i);
}
int main()
{
int t;
int i,a,b;
scanf("%d",&t);
while(t --)
{
scanf("%d",&n);
init();
for(i = 1;i < n;i ++)
{
scanf("%d%d",&a,&b);
build(a,b);
in[b] ++;
}
scanf("%d%d",&p,&q);
tarjan();
}
return 0;
}

2:LCA转RMQ,再st算法:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 20005; int dep[N],pos[N],seq[N],first[N],in[N];
int dp[N][20];
struct node
{
int to,next;
}e[N];
int head[N];
int n,num,p,q,id;
void build(int s,int ed)
{
e[num].to = ed;
e[num].next = head[s];
head[s] = num ++;
} void dfs(int cur,int deep)
{
dep[cur] = deep;
first[cur] = id;
pos[id] = cur;
seq[id ++] = dep[cur];
int i;
for(i = head[cur];i != -1;i = e[i].next)
{
dfs(e[i].to,deep + 1);
pos[id] = cur;
seq[id ++] = dep[cur];
}
}
int rmq()
{
int i,j;
for(i = 1;i <= id;i ++)
dp[i][0] = i;
for(j = 1;(1<<j) <= id;j ++)
{
for(i = 1;(i + (1<<(j - 1))) <= id;i ++)
if(seq[dp[i][j - 1]] < seq[dp[i + (1<<(j - 1))][j - 1]])
dp[i][j] = dp[i][j - 1];
else
dp[i][j] = dp[i + (1<<(j - 1))][j - 1];
}
int tp = first[p];
int tq = first[q];
if(tp > tq)
swap(tp,tq);
int k = floor(log((double)(tq - tp + 1))/log(2.0));
int tmp;
if(seq[dp[tp][k]] < seq[dp[tq - (1<<k) + 1][k]])
tmp = dp[tp][k];
else
tmp = dp[tq - (1<<k) + 1][k];
return pos[tmp];
}
void solve()
{
int i;
id = 1;
for(i = 1;i <= n;i ++)
if(in[i] == 0)
break;
dfs(i,0);
id --;
printf("%d\n",rmq());
}
int main()
{
int i,a,b,t;
freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t --)
{
scanf("%d",&n);
num = 0;
memset(head,-1,sizeof(head));
memset(in,0,sizeof(in));
for(i = 1;i < n;i ++)
{
scanf("%d%d",&a,&b);
build(a,b);
in[b] ++;
}
scanf("%d%d",&p,&q);
solve();
}
return 0;
}

3:倍增算法:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 10005; int dp[N][20],deep[N];
struct node
{
int to,next;
}e[N];
int n,num,p,q;
int head[N],in[N];
void build(int s,int ed)
{
e[num].to = ed;
e[num].next = head[s];
head[s] = num ++;
}
void dfs(int cur,int fa)
{
deep[cur] = deep[fa] + 1;
dp[cur][0] = fa;
int i;
for(i = 1;i < 18;i ++)
dp[cur][i] = dp[dp[cur][i - 1]][i - 1];
for(i = head[cur];i != -1;i = e[i].next)
{
dfs(e[i].to,cur);
}
}
int lca()
{
if(deep[p] < deep[q])
swap(p,q);
int i,j;
for(j = deep[p] - deep[q],i = 0;j;j >>= 1,i ++)
{
if(j&1)
p = dp[p][i];
}
if(p == q)
return q;
for(i = 18;i >= 0;i --)
{
if(dp[p][i] != dp[q][i])
{
p = dp[p][i];
q = dp[q][i];
}
}
return dp[q][0];
}
void solve()
{
int i;
memset(deep,0,sizeof(deep));
for(i = 1;i <= n;i ++)
if(in[i] == 0)
break;
dfs(i,0);
printf("%d\n",lca());
}
int main()
{
int t,i,a,b;
freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t --)
{
scanf("%d",&n);
num = 0;
memset(head,-1,sizeof(head));
memset(in,0,sizeof(in));
for(i = 1;i < n;i ++)
{
scanf("%d%d",&a,&b);
build(a,b);
in[b] ++;
}
scanf("%d%d",&p,&q);
solve();
}
return 0;
}

poj1330Nearest Common Ancestors(LCA小结)的更多相关文章

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

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

  2. poj 1330 Nearest Common Ancestors LCA

    题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...

  3. POJ 1330 Nearest Common Ancestors LCA题解

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

  4. pku 1330 Nearest Common Ancestors LCA离线

    pku 1330 Nearest Common Ancestors 题目链接: http://poj.org/problem?id=1330 题目大意: 给定一棵树的边关系,注意是有向边,因为这个WA ...

  5. poj 1330 Nearest Common Ancestors lca 在线rmq

    Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer scienc ...

  6. Nearest Common Ancestors(LCA)

    Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...

  7. poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)

    LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好 ...

  8. poj----1330Nearest Common Ancestors(简单LCA)

    题目连接  http://poj.org/problem?id=1330 就是构建一棵树,然后问你两个节点之间最近的公共父节点是谁? 代码: /*Source Code Problem: 1330 U ...

  9. POJ-1330--Nearest Common Ancestors(离线LCA)

    LCA离线算法 它需要一次输入所有的询问,然后有根节点开始进行深度优先遍历(DFS),在深度优先遍历的过程中,进行并查集的操作,同时查询询问,返回结果. 题意: 求A ,B两点的最近公共祖先 分析: ...

随机推荐

  1. HashMap的存储结构及原理

    1.HashMap的数据结构(HashMap通过hashcode对其内容进行高速查找,是无序的) 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是两个极端. 数组 :数组的存储区是连续的,占 ...

  2. python-socket 粘包问题

    解决粘包的问题: 1.服务端在发送数据之前,先把发送数据的长度告诉客户端,要发送多少数据,然后客户端根据这个数据的长度循环接收就OK 传输过程: 服务端:     1.send  #数据长度     ...

  3. Proguard 保留native methods的问题

    发现一个奇怪的问题,如果使用下面的配置来keep的话,native的方法还是被删掉了,百思不得其解. -keepclasseswithmembers class * {     native *; } ...

  4. pfile,spfile 初始化参数文件顺序【weber出品】

    一.初始化参数文件 启动实例时会读取初始化参数文件.参数文件有两种类型: 1.服务器参数文件:这是首选类型的初始化参数文件.这是一个由数据库服务器写入或读取的二进制文件,不得手动进行编辑.此文件驻留在 ...

  5. 帝国cms实现自动生成缩略图和自动分页功能

    无论你手工发布,还是采集而来,免不了要进行手工操作弄缩略图,不然标题图片没有,挺烦人的 只需一次设定,就可以在文章编辑框里自动勾选上分页和生成缩略图,免除你次次进行操作的麻烦,好了,废话不多说,上菜“ ...

  6. mysql 套事物实例

    public static DataSet GetPPriceList(string aircompany, string departPort, string arrivePort, string ...

  7. 观《Terminal》之感

    读书笔记系列链接地址http://www.cnblogs.com/shoufengwei/p/5714661.html.        经人推荐,用了几天时间欣赏了这部斯皮尔伯格导演的电影<Te ...

  8. Escape character is '^]'. Connection closed by foreign host.

    今天在用易汇金的接口回调时候,老是回调不到我的机器上面.我的ip通过公网映射,按说是可以访问到我的ip,思考是什么问题. 1.防火墙关闭,不行 2.防火墙开启,但是把自己的端口号改为可以访问(参考:h ...

  9. Tuning Radio Resource in an Overlay Cognitive Radio Network for TCP: Greed Isn’t Good

    好吧,这是09年七月发布在IEEE Communications Magazine的一篇文章. 核心二个词:overlay cognitive radio network,tcp 讲的是,在认知无线网 ...

  10. Unity3D使用NGUI做个弹窗

    主要函数:  NGUITools.AddChild(gameobj, prefab);//gameobj为父对象,prefab为要显示的窗体对象预制 然后写个方法,限制出现多个即可