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 ...
随机推荐
- MFC技术积累——基于MFC对话框类的那些事儿
1. 创建对话框类 (1)打开VC++6.0环境,点击:文件→新建: (2)在弹出的新建对话框中选择:工程→MFC AppWizard (exe)→输入工程名称(例如:功能调试)→工程保存路径名→确定 ...
- Linux OpenGL 实践篇-12-procedural-texturing
程序式纹理 简单的来说程序式纹理就是用数学公式描述物体表面的纹路 .而实现这个过程的着色器我们称之为程序纹理着色器,通常在这类着色器中我们能使用的输入信息也就是顶点坐标和纹理坐标. 程序式纹理的优点 ...
- 利用python递归实现整数转换为字符串
def trans(num): if num // 10 == 0: return '%s'%num else: return trans(num//10)+'%s'%(num%10) a=trans ...
- 思维 || Make It Equal
http://codeforces.com/contest/1065/problem/C 题意:给你n个高度分别为a[i]的塔,每次可以横着切一刀,切掉不多于k个塔,问最少切多少刀才能把塔切的都一样高 ...
- Linux系统GEDIT编译运行C++
作为NOIP第一年强制使用Linux系统的考生,真的很难受,被迫还要学一波Linux系统. 正常的Windows对于较基础的程序员来说非常方便好用,但是对于高级程序员来说就是一个坑,于是就有了Linu ...
- hibernate4整合spring3.1的过程中的异常问题
(1)hibernate4整合spring3.1的过程中,发现了java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider异常 ...
- SSH整合JAR包详解
如果要使用连接池,添加JAR : c3p0-0.9.1.2.jar
- Bootstrap历练实例:成功按钮
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- shell脚本,实现奇数行等于偶数行。
请把如下字符串stu494e222fstu495bedf3stu49692236stu49749b91转为如下形式:stu494=e222fstu495=bedf3stu496=92236stu497 ...
- UISearchBar的使用
searchBar = [[UISearchBar alloc] initWithFrame: CGRectMake(0.0, 0.0, self.view.bounds.size.width, 40 ...