Nearest Common Ancestors(poj 1330)
题意:给定一棵树,询问两个节点的最近公共祖先。
输入:第一行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)的更多相关文章
- Nearest Common Ancestors (POJ 1330)
A rooted tree is a well-known data structure in computer science and engineering. An example is show ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- POJ 1330 Nearest Common Ancestors(裸LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39596 Accept ...
- Nearest Common Ancestors(LCA板子)
题目链接:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 1000 ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- POJ 1330 Nearest Common Ancestors(Tree)
题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...
- poj 1330 Nearest Common Ancestors(LCA 基于二分搜索+st&rmq的LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30147 Accept ...
- POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14902 Accept ...
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
随机推荐
- 团队作业-Beta冲刺第三天
这个作业属于哪个课程 <https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1> 这个作业要求在哪里 <https ...
- mac系统下android studio创建手机模拟器
打开android studio,点击右上角的模拟器图标,打开“Android Virtual Device Manager” 窗口,如下图 点击“Create Virtual Device”,在 ...
- Quartz监听的端口
上海移通短信网关:556重庆移动短信网关:557消息中心后台维护服务:558网页订单数据同步服务:559基础数据同步程序:560短信数据扣除服务:565基础数据维护服务:589推送数据抓取服务:222 ...
- OAuth授权之回调accessToken
具体说明见新浪官方文档 http://open.weibo.com/wiki/Oauth2/access_token 具体实现 第一步 打开回调页面 // 宏定义client_id #define ...
- UINavgationController
UINavigationBar和UINavigationItem是iOS开发中常用的控件. 1.设置导航栏标题 self.title = @"iOS开发:iOSDevTip"; ...
- PLAYGROUND 可视化
PLAYGROUND 可视化 由 王巍 (@ONEVCAT) 发布于 2015/09/23 在程序界,很多小伙伴都会对研究排序算法情有独钟,并且试图将排序执行的过程可视化,以便让大家更清晰直观地了解算 ...
- ubuntu 安装 php7.2
sudo apt-get install software-properties-common python-software-properties sudo add-apt-repository p ...
- perl学习 之:my local our
范围声明 和全局声明类似,词法范围声明也是在编译时起作用的.和全局声明不同的是,词法范围声明的作用范围是从声明开始到闭合范围的最里层(块,文件,或者 eval--以先到者为准).这也是为什么我们称它为 ...
- 【git】自动换行转换autocrlf
#####windows git config --global core.autocrlf true #####linux git config --global core.autocrlf inp ...
- mysql 慢查询日志 mysqldumpslow 工具
文章来源:https://www.cnblogs.com/hello-tl/p/9229676.html 1.使用Mysql慢查询日志配置 查看慢查询日志是否开启 OFF关闭 ON开启 show va ...