多校第七场考了一道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. POJ 3614 Sunscreen 贪心

    题目链接: http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MSMemory Limit: 65536K 问题描述 to avoid ...

  2. spring配置事务

    一.配置JDBC事务处理机制 <!-- 配置Hibernate事务处理 --> <bean id="transactionManager" class=" ...

  3. AnkhSVN 安装

    为 visual Studio 2013 添加 AnkhSVN 步骤 到 https://ankhsvn.open.collab.net/downloads 下载 2.6x或以上 安装 AnkhSvn ...

  4. PHP中$_SERVER获取当前页面的完整URL地址

    PHP中$_SERVER获取当前页面的完整URL地址,其实很简单,主要是通过$_SERVER超全局变量来实现的. 具体PHP中$_SERVER获取当前页面的完整URL地址如下. #测试网址:     ...

  5. Robot Framework 环境搭建

    一.下载软件 1.安装Python 到官网,下载Python 2.7.9:https://www.python.org/downloads/,最好选择32位版本的(64位系统也支付32位版本),然后安 ...

  6. Selenium网址

    Selenium官网:http://www.seleniumhq.org/ Selenium火狐插件地址:http://release.seleniumhq.org/selenium-ide/ 浏览器 ...

  7. MySQL 语句级避免重复插入—— Insert Select Not Exist

    想要插入一条数据,要避免重复插入,又不想折腾两回数据库连接操作,可以参考如下办法. INSERT INTO table(column1,column2,column3 ...columnN) SELE ...

  8. crontab 指定执行用户

    linux下可以通过配置crontab来定时执行任务,执行体可以是一条系统命令或自己写的一个脚本,同时可以指派用户来执行.配置crontab有两种方法.方法1.使用crontab命令,例如添加一个新的 ...

  9. jquery cookie 用法

    jquery cookie 用法 $.cookie("name","value","options")  当不设置options时,此coo ...

  10. lintcode:落单的数

    题目: 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 样例 给出 [1,2,2,1,3,4,3],返回 4 挑战 一次遍历,常数级的额外空间复杂度 ...