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 ...
随机推荐
- (十二)mybatis之动态代理
mybatis之动态代理的应用 在前文(https://www.cnblogs.com/NYfor2018/p/9093472.html)我们知道了,Mybatis的使用需要用到Mapper映射文件, ...
- 46.Maximum Product Subarray(最大乘积子数组)
Level: Medium 题目描述: Given an integer array nums, find the contiguous subarray within an array (con ...
- 获得stixel的gt数据
这是论文中的作者博客地址https://sites.google.com/site/danmlevi/ 这是作者现在的博客地址https://sites.google.com/view/danlevi ...
- DP玄学优化——斜率优化
--以此博客来悼念我在\(QBXT\)懵逼的时光 \(rqy\; tql\) (日常%\(rqy\)) 概念及用途 斜率优化是\(DP\)的一种较为常用的优化(据说在高中课本里稍有提及),它可以用于优 ...
- MySQL 查询优化之 Index Condition Pushdown
MySQL 查询优化之 Index Condition Pushdown Index Condition Pushdown限制条件 Index Condition Pushdown工作原理 ICP的开 ...
- GIMP里的Path移动,旋转,翻转操作
1/Path的移动: 快捷键Ctrl+Move Tool 2/Path的旋转: 选择Rotate Tool,在Path中选择,出现十字圈. Angel下的滑块调节一定的角度,在合适的位置即可. 3/P ...
- systemverilog 之interface/timing region/program
1.connecting the testbench and the design 2.verilog connection review 3.systemverilog interfaces 4.s ...
- 【java】 field 和 variable 区别及相关术语解释
Having said that, the remainder of this tutorial uses the following general guidelines when discussi ...
- python爬虫基础06-常见加密算法
Python与常见加密方式 前言 数据加密与解密通常是为了保证数据在传输过程中的安全性,自古以来就一直存在,古代主要应用在战争领域,战争中会有很多情报信息要传递,这些重要的信息都会经过加密,在发送到对 ...
- js事件,操作页面文档,计算后样式,数据类型
js:运行在浏览器的脚本语言 js引入 1.行间式:存在于行间事件中 <div id="div" onclick="this.style.color="r ...