题目请戳这里

题目大意:意如其名。

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

不过对于菜鸟而言,还是老老实实练习一下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. Android开源代码解读-基于SackOfViewAdapter类实现类似状态通知栏的布局

    一般来说,ListView的列表项都会采用相同的布局,只是填充的内容不同而已,这种情况下,Android提供了convertView帮我们缓存列表项,达到循环利用的目的,开发者也会使用ViewHold ...

  2. Android基于WIFI实现电脑和手机间数据传输的技术方案研究

    Android手机和电脑间基于wifi进行数据传输,从技术上讲,主要有两种方案: 一种是通过ftp协议实现,Android手机作为数据传输过程中的ftp服务器: 一种是通过http协议实现.Andro ...

  3. C#递归算法详解

    递归呢就是自己调用自己,在搜索文件夹下的文件和目录时也能用到,我这里就写一个简单的递归,代码如下: /// <summary> /// 递归算法 /// </summary> ...

  4. SpringTest2

    Spring 框架第二天 AOP切面编程 今天重点内容: 1. 什么是AOP ? AOP实现原理是怎样的? AOP相关术语 2. AOP底层实现 (了解) ----- JDK动态代理. Cglib动态 ...

  5. Ubuntu + hadoop2.6.0下安装Hive

    第一步:准备hive和mysql安装包 下载hive 1.1.1 地址:http://www.eu.apache.org/dist/hive/ 下载Mysql JDBC 5.1.38驱动:http:/ ...

  6. JS中的for和for in循环

    1.for循环通常用来遍历数组或类数组对象 模式1:长度缓存 for(var i=0,max=arr.length;i<max;i++){ //your code } 模式2:逐减,与零比较比与 ...

  7. 关于TP的RBAC的使用

    如果你是一个TP的重度使用者的话,请自动略过笔者以下文字 权限管理我在成为一个开发者1年半内,接触过2种,一种就是数学方式 比如我们文件夹权限的755这种权限管理的方式,这种二进制的权限管理的方式 无 ...

  8. [模拟炉石](一)让游戏过程显示到cocos2d中

    在上篇中,如果运行了fireplace的tests/full_game.py,这个程序将一个游戏过程在终端上运行完成,可以看到整个过程,那么第一步要做的就是将这个过程显示到cocos2d创建的场景中去 ...

  9. 解决Windows 7/win8 使用VMware虚拟机的NAT 不能上网

    最近在学习linux系统,在使用debian6更新源的时候,发现Nat模式上网就是配置不了.而内外网可以ping通.所以很苦恼.最后终于解决了. 以下操作在VMware10下进行 1.首先要设置一下 ...

  10. Codeforces 486D D. Valid Sets

    http://codeforces.com/contest/486/problem/D 题意:给定一棵树,点上有权值,以及d,要求有多少种联通块满足最大值减最小值小于等于d. 思路:枚举i作为最大的点 ...