题意:给定一棵树,询问两个节点的最近公共祖先。

输入:第一行T,表示测试组数。

每组测试数据包含一个n,表示节点数目,下面n-1行是连接的边,最后一行是询问

输出:共T行,代表每组的测试结果

/*
倍增LCA
注意这是树,所以边是单向的,深搜的时候从根节点开始搜
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#define M 10010
#define S 20
using namespace std;
int head[M],deep[M],fa[M][S+],in[M],num,n;
struct node
{
int u,v,pre;
};node e[M*];
void add(int x,int y)
{
++num;
e[num].u=x;
e[num].v=y;
e[num].pre=head[x];
head[x]=num;
}
void dfs(int now,int from,int c)
{
fa[now][]=from;deep[now]=c;
for(int i=head[now];i;i=e[i].pre)
if(e[i].v!=from)
dfs(e[i].v,now,c+);
}
void get_fa()
{
for(int j=;j<=S;j++)
for(int i=;i<=n;i++)
fa[i][j]=fa[fa[i][j-]][j-];
}
int get_same(int a,int t)
{
for(int i=;i<=S;i++)
if(t&(<<i))
a=fa[a][i];
return a;
}
int LCA(int a,int b)
{
if(deep[a]<deep[b])swap(a,b);
a=get_same(a,deep[a]-deep[b]);
if(a==b)return a;
for(int i=S;i>=;i--)
if(fa[a][i]!=fa[b][i])
{
a=fa[a][i];
b=fa[b][i];
}
return fa[a][];
}
void init()
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
in[y]++;
add(x,y);
}
int p;
for(int i=;i<=n;i++)
if(!in[i])
{
p=i;
break;
}
dfs(p,p,);
get_fa();
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",LCA(a,b));
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(e,,sizeof(e));
memset(head,,sizeof(head));
memset(deep,,sizeof(deep));
memset(fa,,sizeof(fa));
memset(in,,sizeof(in));
num=;
init();
}
return ;
}

Nearest Common Ancestors(poj 1330)的更多相关文章

  1. Nearest Common Ancestors (POJ 1330)

    A rooted tree is a well-known data structure in computer science and engineering. An example is show ...

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

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

  3. POJ 1330 Nearest Common Ancestors(裸LCA)

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

  4. Nearest Common Ancestors(LCA板子)

    题目链接:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 1000 ...

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

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

  6. POJ 1330 Nearest Common Ancestors(Tree)

    题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...

  7. poj 1330 Nearest Common Ancestors(LCA 基于二分搜索+st&rmq的LCA)

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

  8. POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)

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

  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. (七)mybatis之创建SqlSession

    前文提到了SqlSessionFactory接口,可以用来生产SqlSession.而SqlSession其实也是一个接口类,也就是起到一个门面的角色.那么谁是真正干活的呢?------Executo ...

  2. Go 1.4 正式版发布,官方正式支持 Android

    Go 1.4 正式发布啦,是第五个 Go 的稳定版本,与上一个稳定版本 Go 1.3 相隔 6 个月.Go 1.4 包括一些小的语言改进,支持更多的操作系统和处理器架构:改进了工具链和库.同时,Go ...

  3. UESTC cdoj 619 吴神,人类的希望 (组合数学)

    枚举盒子的个数,先把总数n减去掉box*k保证每个盒子至少有k个小球,剩下的小球放入盒子中可以为空, 加入box个小球保证每个盒子至少有一个小球,问题转化成不可区分小球放入不可区分盒子非空的方案数. ...

  4. MIPS简单入门

    What ‘s the MIPS? 汇编语言 汇编语言是一类语言的总称,因处理器不同,而对应的不同机器的指令集也不同,产生了很多种汇编语言. 目前最流行的是ARM,MIPS,x86.ARM用于大量的移 ...

  5. 修改visual studio setup 安装顺序(解决新版安装包无法自动移除老版本程序的问题)

    背景 visual studio setup 支持自动删除之前版本的安装,需要设置RemovePreviousVersions = true, DetectNewerInstalledVersion ...

  6. selenium--Xpath定位

    前戏 前面介绍过了七种定位方式,今天来介绍最后一种,也是最强大,本人最常用的定位方式xpath Xpath 即为 xml 路径语言,它是一种用来确定 xml 文档中某部分位置的语言.Xpath 基于 ...

  7. Perl 使用哈希的引用

    $ref = \%hash_clomnname_linevalue; $hash_of_whole_table{$table_name} = {%$ref};

  8. CAS (Compare and Swap)

    synchronized是悲观锁 注意:实现了CAS的有原子类(AtomicInteger,AtomicLong,等等原子类) CAS 是乐观锁,一种高效实现线程安全性的方法 1.支持原子更新操作,适 ...

  9. shell-code-exerciese-1

    &&&&&&&&&&&&&&&&&&&& ...

  10. nw335 debian sid x86-64 --3 linux内核自带

    nw335 debian sid x86-64 --3  linux内核自带