题目链接:

id=1330">传送门

在线算法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 40010; struct nod{
int to,next,w;
}edge[maxn*2]; int head[maxn],ip,tot;
bool vis[maxn];
int R[maxn*2],ver[maxn*2];
int dp[maxn*2][25];
int first[maxn];
int dis[maxn];
bool isroot[maxn]; void init(){
memset(head,-1,sizeof(head));
memset(isroot,0,sizeof(isroot));
memset(vis,false,sizeof(vis));
dis[1]=0,ip=0,tot=0;
} void add(int u,int v){
edge[ip].to=v;
edge[ip].next=head[u];
head[u]=ip++;
}
/***
ver[i]=x:第i个点是x.
first[i]=x: 点i第一次出现的位置是x
R[i]=x:第i个点的深度为x;
dis[i]=x;点i到根节点的距离为x.
***/
void dfs(int u,int dept){
vis[u]=true,ver[++tot]=u,first[u]=tot,R[tot]=dept;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(!vis[v]){
dfs(v,dept+1);
ver[++tot]=u,R[tot]=dept;
}
}
} void ST(int n){
for(int i=1;i<=n;i++) dp[i][0]=i;
for(int i=1;(1<<i)<=n;i++){
for(int j=1;j+(1<<i)<=n;j++){
int a = dp[j][i-1],b=dp[j+(1<<(i-1))][i-1];
if(R[a]<R[b]) dp[j][i]=a;
else dp[j][i]=b;
}
}
} int RMQ(int l,int r){
int k=0;
while(1<<(k+1)<=r-l+1)
k++;
int x = dp[l][k], y=dp[r-(1<<k)+1][k];
if(R[x]<R[y]) return x;
else return y;
} int LCA(int u,int v){
u=first[u],v=first[v];
if(u>v) swap(u,v);
return ver[RMQ(u,v)];
} int main(){
int t,n,m;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
init();
for(int i=0;i<n-1;i++){
int u,v,w;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
isroot[v]=1;
}
int x,y;
scanf("%d%d",&x,&y);
int root;
for(int i=1;i<=n;i++){
if(!isroot[i]){
root=i;
break;
}
}
dfs(root,1);
ST(n*2-1);
printf("%d\n",LCA(x,y));
}
return 0;
}

离线算法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 40010; struct nod{
int u,v,next,w,lca;
}edge[maxn*2],edge1[maxn]; int par[maxn],ancestors[maxn];
int head[maxn];
int dis[maxn],ip;
int x,y;
bool vis[maxn];
bool root[maxn]; void init(){
memset(head,-1,sizeof(head));
memset(vis,false,sizeof(vis));
for(int i=0;i<maxn;i++) root[i]=true;
for(int i=1;i<maxn;i++) par[i]=i;
ip=0;
} int find_par(int x){
if(x!=par[x]) return par[x]=find_par(par[x]);
return par[x];
} void Union(int u,int v){
u=find_par(u);
v=find_par(v);
if(u!=v) par[v]=u;
} void add(int u,int v){
edge[ip].v=v;
edge[ip].next=head[u];
head[u]=ip++;
} bool ans; void tarjan(int u){
vis[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next){
int v = edge[i].v;
if(!vis[v]){
dis[v]=dis[u]+edge[i].w;
tarjan(v);
Union(u,v);
}
}
if(u==x&&vis[y]&&!ans){
ans=1;
printf("%d\n",find_par(y));
return;
}
if(u==y&&vis[x]&&!ans){
ans=1;
printf("%d\n",find_par(x));
return;
}
} int main()
{
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
init();
for(int i=0;i<n-1;i++){
int u,v;
scanf("%d%d",&u,&v);
root[v]=false;
add(u,v);
add(v,u);
}
ans=0;
scanf("%d%d",&x,&y);
for(int i=1;i<=n;i++){
if(root[i]){
tarjan(i);
break;
}
}
}
return 0;
}

HDU 1330 Nearest Common Ancestors(求两个点的近期公共祖先)的更多相关文章

  1. poj 1330 Nearest Common Ancestors 求最近祖先节点

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

  2. POJ 1330 Nearest Common Ancestors(求最近的公共祖先)

    题意:给出一棵树,再给出两个节点a.b,求离它们最近的公共祖先.方法一: 先用vector存储某节点的子节点,fa数组存储某节点的父节点,最后找出fa[root]=0的根节点root.      之后 ...

  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 Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %l ...

  5. pku 1330 Nearest Common Ancestors LCA离线

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

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

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

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

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

  8. LCA POJ 1330 Nearest Common Ancestors

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

  9. POJ 1330 Nearest Common Ancestors(lca)

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

随机推荐

  1. FreeMarker中if标签内的判断条件

    reeMarker中的<#if>标签除了里面直接判断 boolean 类型的变量外,也可以进行表达式判断,有几个细节记录一下 1. 判断对象是否存在(null) 经常会用到,如果对象 != ...

  2. bzoj3294

    感觉自己就是不怎么擅长计数的问题 设f[k,i,j]表示前k种颜色占据了i行j列的方案 g[k,i,j]表示第k种颜色占据了i行j列的方案,注意要减去并没占据满i行j列的情况 然后转移就很好写了 像这 ...

  3. urllib

    urllib Python 标准库 urllib2 的使用细节 python 2.x 3.x from urllib import request with request.urlopen('http ...

  4. Xcode8安装不成功, 需要升级系统. The operation couldn't be completed. cpio read error

    https://developer.apple.com/library/prerelease/content/documentation/DeveloperTools/Conceptual/Whats ...

  5. 关于Memcache mutex设计模式的.net实现

    之前在网上看过memcache-mutex的场景分析和实现代码,这里将.net方式加以实现,当然这里主要是依据原文的伪代码照猫画虎,以此做为总结及记录.如果您对相应实现感兴趣可以尝试使用本文提供的代码 ...

  6. log4j和web.xml配置webAppRootKey 的问题

    在tomcat下部署两个或多个项目时,web.xml文件中最好定义webAppRootKey参数,如果不定义,将会缺省为“webapp.root”,如下: <!-- 应用路径 --> &l ...

  7. MAC下显示或者隐藏文件的命令

    显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏Mac隐藏文件的命令:defaults writ ...

  8. 50道经典的JAVA编程题(36-40)

    50道经典的JAVA编程题(36-40),今天晚上心情压抑,不爽,继续做题,管它明天考试,我继续我的java,一个周末都在看微机原理看得的很头疼啊~明天该挂科就挂吧,不在乎了~~~ [程序36] Ar ...

  9. CoreSeek中文检索引擎

    目的:安装coreseek中文检索引擎,配置MySQL数据库访问接口,使用PHP程序实现中文检索. CoreSeek官方网站: http://www.coreseek.cn/ http://www.c ...

  10. hdoj 1162 Eddy's picture

    并查集+最小生成树 Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...