题目链接:

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. HashMap和Hashtable的区别(1)

    导读: 1 HashMap不是线程安全的 hastmap实现了map接口,是将键映射到值的对象,其中键和值都是对象,并且不能包含重复键,但可以包含重复值.HashMap允许null key和null ...

  2. poj 1364 King(差分约束)

    题目:http://poj.org/problem?id=1364 #include <iostream> #include <cstdio> #include <cst ...

  3. 函数 buf_chunk_init

    http://www.tuicool.com/articles/3QbYfm http://www.360doc.com/content/13/1216/17/12904276_337644353.s ...

  4. Java数组你知多少?

    下面我带大家一起加深一下对Java数组的认识: 1.理解数组 数组也是一种数据类型,本身就是一种引用类型,我们从它的初始化方法,通过关键字new去完成定义及初始化就可以知道. 数组的长度是不变的,一旦 ...

  5. 十大流行Linux发行版

    [2013年5月13日 51CTO外电头条]Linux是功能强大的常用操作系统之一,目前它在计算机操作系统领域的发展速度越来越快.它提供了出色的性能和速度.Linux用起来非常稳定而可靠.它还提供了几 ...

  6. Codeforces Round #306 (Div. 2)

    A. Two Substrings You are given string s. Your task is to determine if the given string s contains t ...

  7. python Image PNG getpixel R/G/B/A

    # python Image PNG getpixel R/G/B/A# # 说明: # 本文主要是记录python中如何使用Image模块进行基本的图像R.G.B.A值得获取. # 为后续的rasp ...

  8. dubbo + zookeeper 环境搭建

    一.zookeeper windows部署 1.下载安装 到官网下载解压版后解压至F:\server\zookeeper-3.4.8,剩下为文件配置工作: 2.本地伪集群 1) 在F:\server\ ...

  9. Asp.Net 高性能框架 SqlSugar.ORM 2.3

    一.前言 SqlSugar从去年到现在已经一年了,版本从1.0升到了现在的2.3 ,这是一个稳定版本 ,有数家公司已经项目上线,在这里我将SqlSugar的功能重新整理成一篇新的贴子,希望大家喜欢. ...

  10. bzoj 2588 Spoj 10628. Count on a tree(主席树)

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...