多校第七场考了一道lca,那么就挑一道水题学习一下吧= =

最简单暴力的方法:建好树后,输入询问的点u,v,先把u全部的祖先标记掉,然后沿着v->rt(根)的顺序检查,第一个被u标记的点即为u,v的公共祖先。

标记的时候又犯老毛病了:while,do while都不对,最后还是while(1)了T^T

 #include<cstdio>
#include<cstring> const int MAXN=; int p[MAXN],vis[MAXN]; int main()
{
int T,n,u,v;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(vis,,sizeof(vis));
memset(p,-,sizeof(p));
for(int i=;i<n-;i++){
scanf("%d%d",&u,&v);
p[v]=u;
}
scanf("%d%d",&u,&v);
while()
{
vis[u]=;
if(p[u]==-)
break;
u=p[u];
}
while(vis[v]!=)
v=p[v];
printf("%d\n",v);
}
return ;
}
/*
10
3
1 2
1 3
2 3
*/

然后是tarjin了:

自己先照着推了一遍代码,才把图示给看明白:http://www.csie.ntnu.edu.tw/~u91029/LowestCommonAncestor.html

可以处理多组询问

因为是以dfs为框架,所以先处理子树,依次将当前点u的每棵子树加入当前点的集合,之后对当前点进行询问(u,v)。若v已被标记,由于是dfs,所以最近公共祖先包含v的子树必然已经完成了搜索,那么v所在集合的祖先 find(v) 就是询问(u,v)的解。又因为是dfs,所以子树内的询问必然已经完成。

注意:询问(u,v)要正反向各加一遍,因为标记vis有先后顺序,而我们不清楚u,v的被访问的顺序。搜索到u时v尚未被标记,会放弃此次询问。所以不必担心同一次询问被完成了两次,其中一次会被舍弃。

关键是要理解先处理dfs,再合并集合。

 #include<cstdio>
#include<cstring>
#include<vector>
#define clr(a,m) memset(a,m,sizeof(a))
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std; const int MAXN=; struct Edge{
int v,next;
Edge(){}
Edge(int _v,int _next):v(_v),next(_next){}
}edge[MAXN<<]; vector<int>query[MAXN]; int p[MAXN],vis[MAXN],ancestor[MAXN];
int head[MAXN],tol; void init(int n)
{
tol=;
clr(head,-); rep(i,,n){
query[i].clear();
}
} void add(int u,int v)
{
edge[tol]=Edge(v,head[u]);
head[u]=tol++;
} int build(int n)
{
int u,v;
init(n);
clr(vis,);
rep(i,,n-){
scanf("%d%d",&u,&v);
vis[v]=;
add(u,v);
}
rep(i,,){
scanf("%d%d",&u,&v);
query[u].push_back(v);
query[v].push_back(u);
}
rep(i,,n)
if(!vis[i])
return i;
} int find(int x)
{
return (x==p[x])?x:(p[x]=find(p[x]));
} void dfs(int x)
{
vis[x]=;
for(int i=head[x];i!=-;i=edge[i].next)
{
int v=edge[i].v;
dfs(v);
p[v]=x;
} int siz =query[x].size()-;
rep(i,,siz)
{
if(vis[query[x][i]]){
printf("%d\n",find(query[x][i]));
return ;
}
}
} void LCA(int rt,int n)
{
clr(vis,);
rep(i,,n)
p[i]=i;
dfs(rt);
} int main()
{
int T,n,u,v;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int rt=build(n);
LCA(rt,n);
}
return ;
}

有一点要注意的:query[x].size()的返回值竟然不是 int 型的= = ,不信可以把 siz 省略掉,在循环内打印一下,惊喜的发现 for(int i=0;i<-1;i++) 这种东西竟然能够进入循环。强制转换(int)也能解决。

再附上一段代码,是实现全部询问的,当然不适合这道题,MAXN=11111,MLE我竟然还反应了半天。。

 void dfs(int x,int n)
{
vis[x]=;
for(int y=head[x];y!=-;y=edge[y].next)
{
int v=edge[y].v;
dfs(v,n);
p[v]=x;
}
rep(y,,n)
if(vis[y])
lca[x][y]=lca[y][x]=find(y);
}

poj 1330 Nearest Common Ancestors(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 倍增)

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

  3. POJ 1330 Nearest Common Ancestors LCA题解

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

  4. poj 1330 Nearest Common Ancestors lca 在线rmq

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

  5. poj 1330 Nearest Common Ancestors LCA

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

  6. POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)

    /* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...

  7. POJ 1330 Nearest Common Ancestors(LCA模板)

    给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...

  8. POJ 1470 Closest Common Ancestors(LCA 最近公共祖先)

    其实这是一个裸求LCA的题目,我使用的是离线的Tarjan算法,但是这个题的AC对于我来说却很坎坷……首先是RE,我立马想到数组开小了,然后扩大了数组,MLE了……接着把数组调整适当大小,又交了一发, ...

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

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

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

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

随机推荐

  1. log4j安装与简介

    问题描述:     在应用程序中添加日志记录总的来说基于三个目的:    (1) 监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工作:     (2) 跟踪代码运行时轨迹,作为日 ...

  2. 【BZOJ】【1051】【HAOI2005】受欢迎的牛

    按B->A连边,tarjan缩点,然后找入度为0的连通分量,如果有1个,则ans=size[i],如果大于一个则ans=0: 当然如果按A->B连边就是找出度为0的(表示没有被它喜欢的,这 ...

  3. [转载]C#获取本机IPv4地址

    C#获取本机IP地址在C#1.0之后都使用下面的这种形式: IPHostEntry ipe = Dns.GetHostEntry(Dns.GetHostName()); IPAddress ipa=i ...

  4. UDP TCP 消息边界

    先明确一个问题,如果定义了一个数据结构,大小是,比方说 32 个字节,然后 UDP 客户端连续向服务端发了两个包.现在假设这两个包都已经到达了服务器,那么服务端调用 recvfrom 来接收数据,并且 ...

  5. IMP不到指定的表空间

    ==============================================================================只导dmp文件中的几个表数据,解决导入时ta ...

  6. HDU4725 The Shortest Path in Nya Graph SPFA最短路

    典型的最短路问题,但是多了一个条件,就是每个点属于一个layer,相邻的layer移动,如x层移到x+1层需要花费c. 一种显而易见的转化是我把这些边都建出来,但是最后可能会使得边变成O(n^2); ...

  7. HDU 2544 最短路(模板题)

    求1到N的最短路径,模板题,以1为源点,用dijkstra算法(可以用优先级队列优化) #include <iostream> #include <algorithm> #in ...

  8. CSS 的命名和书写

    CSS书写顺序 1.位置属性(position, top, right, z-index, display, float等) 2.大小(width, height, padding, margin) ...

  9. POJ 3318 Matrix Multiplication(矩阵乘法)

    题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. #inc ...

  10. POJ3253Babelfish

    http://poj.org/problem?id=3253 就是一个哈夫曼树,题目下边有提示,所以题意还是好理解的 #include<cstdio> #include<cstrin ...