poj 1330 Nearest Common Ancestors 裸的LCA
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 10010
using namespace std;
int dep[N],farther[N],vi[N],sec[N],Md,index[N],e,set[N];
struct Edge{
int to,next;
}edge[N];
void addedge(int from,int to)
{
edge[e].to=to;
edge[e].next=index[from];
index[from]=e++;
}
void dfs(int u)
{
int i,now;
for(i=index[u];i!=-;i=edge[i].next)
{
now=edge[i].to;
dep[now]=dep[u]+;
farther[now]=u;
dfs(now);
}
Md=max(Md,dep[u]);
}
void make_sec(int u,int Sec)
{
int i,now;//cout<<u<<" "<<Sec<<endl;
if(dep[u]<Sec)
sec[u]=;
else
{
if(dep[u]%Sec==)
sec[u]=farther[u];
else
sec[u]=sec[farther[u]];
}
for(i=index[u];i!=-;i=edge[i].next)
{
now=edge[i].to;
make_sec(now,Sec);
} }
int LCA(int a,int b)
{
while(sec[a]!=sec[b])
{
if(dep[a]>dep[b])
a=sec[a];
else
b=sec[b];
}
while(a!=b)
{
if(dep[a]>dep[b])
a=farther[a];
else
b=farther[b];
}
return a;
}
int find(int x)
{
if(x!=set[x])
set[x]=find(set[x]);
return set[x];
}
void merg(int a,int b)
{
int x,y;
x=find(a);
y=find(b);
set[y]=x;
}
void init()
{
e=;
memset(dep,,sizeof(dep));
memset(sec,,sizeof(sec));
memset(index,-,sizeof(index));
memset(farther,,sizeof(farther));
for(int i=;i<=N;i++)
set[i]=i;
}
int main()
{
int t,i,j,a,b,n;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d",&n);
for(i=;i<n-;i++)
{
scanf("%d%d",&a,&b);
merg(a,b);
addedge(a,b);
}
scanf("%d%d",&a,&b);
dfs(find());
make_sec(find(),sqrt(1.0*Md));//cout<<"ok"<<endl;
printf("%d\n",LCA(a,b));
}
return ;
}
poj 1330 Nearest Common Ancestors 裸的LCA的更多相关文章
- 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/DFS
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19919 Accept ...
- POJ 1330 Nearest Common Ancestors(Tarjan离线LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
- POJ.1330 Nearest Common Ancestors (LCA 倍增)
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...
- POJ 1330 Nearest Common Ancestors 倍增算法的LCA
POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...
- 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(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
- LCA POJ 1330 Nearest Common Ancestors
POJ 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24209 ...
- POJ 1330 Nearest Common Ancestors 【LCA模板题】
任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000 ...
随机推荐
- MySQL alter
1:删除列 ALTER TABLE [表名字] DROP [列名称] 2:增加列 ALTER TABLE [表名字] ADD [列名称] INT NOT NULL COMMENT '注释说明' 3: ...
- C++ delete operator做了什么事
1.C++中的delete operator做了两件事:调用析构方法和调用operator delete释放内存. 2.考虑析构方法,如果析构方法是虚方法,调用指针真实类型的析构方法,否则调用表面类型 ...
- uoj #118. 【UR #8】赴京赶考 水题
#118. [UR #8]赴京赶考 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/118 Description ...
- js实现按回车自行提交
<script type="text/javascript"> document.onkeydown = function (e) { var theEvent = w ...
- ChRoomtst
https://github.com/JawaJedi/ChRoomtst
- 安卓 SQLite数据库操作实例
前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...
- swift 3.0基本数据语法
swift 3.0 字符串的介绍 OC和Swift中字符串的区别 在OC中字符串类型时NSString,在Swift中字符串类型是String OC中字符串@"",Swift中字符 ...
- MYSQL: Handler_read_%参数说明
环境: 表t_feed_idx(user_id bigint, feed_id bigint, KEY (`user_id`,`feed_id`)) engine=innodb;表t_feed_i ...
- Javascript实现鼠标框选元素后拖拽被框选的元素
之前需要做一个框选元素后拖拽被框选中的元素功能,在网上找资料做了一些修改,基本达到了需要的效果,希望对也需要实现框选后拖拽元素功能的人有用. 页面加载后效果 框选后的内容可以拖拽,如下图: 代码下载
- 清除DataTable中的空行记录
第一种方法: string filter = ""; ; i < dt.Columns.Count; i++) { ) filter += dt.Columns[i].Col ...