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

输入:第一行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. 第二次团队作业-PANTHER考勤系统需求分析

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1 这个作业要求在哪里 https://edu.cnblo ...

  2. JS中的事件、事件冒泡和事件捕获、事件委托

    https://www.cnblogs.com/diver-blogs/p/5649270.html https://www.cnblogs.com/Chen-XiaoJun/p/6210987.ht ...

  3. Catalan 数

    概要 在一些面试的智力题中会遇到此数的变形,如果完全不了解,直接想结果是很困难的,故在此简单介绍一下.   基本定义 Catalan 数的定义根据不同的应用环境有很多不同的定义方式,下面给出一个.   ...

  4. JAVA中IP和整数相互转化(含有掩码的计算)

    import java.net.InetAddress;/** * 用于IP和整数之间的相互转换 * @author Andy.Wang * */public class IPv4Util {    ...

  5. 拼凑json的实例

    一,拼凑  json 串 并传到后台 function writeToJson(num){ var i = 1; var jsonData = "["; for(i=1;i< ...

  6. 人脸识别中的检测(在Opencv中加入了QT)

    #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include & ...

  7. ios之UISegmentedcontol

    初始化UISegmentedControl NSArray *arr = [[NSArray alloc]initWithObjects:@"轻拍",@"长按" ...

  8. 文件操作-cd

    cd命令是linux实际使用当中另一个非常重要的命令,本文就为大家介绍下Linux中cd命令的用法. 转载自 https://www.cnblogs.com/waitig/p/5880719.html ...

  9. js 字符串加密

    加密: 1.获得要加密的字符串:var str=input.value; 2.转化: for(var i=0;i<str.length;i++){ str+=String.fromCharCod ...

  10. 浅谈Session与Cookie的关系

    一.概念理解: 首先cookie是服务端识别客户的唯一标识的依据,客户在访问网站时候,服务端为了记住这个客户,会在服务端按照它的规则制作一个cookie数据,会将这个cookie数据保留在服务端一段时 ...