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 ...
随机推荐
- application与cache
每个项目都有一些全局,常用的信息,而这些信息如果在每次使用时都载入,那必将耗费很大的资源,特别是对访问压力大的系统.因此,这个情况中,把这些全局信息放到缓存中是很必要的,放在缓存中可以使得数据能够很快 ...
- sqlserver:同一数据库内负责表结构。
select * into 新表 from 旧表 where 1=2; ## where 表示要携带的数据,随便写个恒不等式就可以只复制表结构不携带数据.
- c++中智能输出文件
首先我们要为每一时间步,设置一个文件名: ] = "; itoa(time,timestr,); std::string s; s += timestr; std::string path ...
- 去除移动端 a标签 点击有一个 阴影效果
outline: none;appearance:none; -webkit-tap-highlight-color: transparent;
- UVA1151
//感觉刘汝佳老师的思维真的太厉害了orz /*摘录书上的一段话: 只需一个小小的优化即可降低时间复杂度:先求一次原图(不购买任何套餐)的最小生 成树,得到n-1条边,然后每次枚举完套餐后只考虑套餐中 ...
- ExtJs内的datefield控件选择日期过后的事件监听select
[摘要]: 选择时间过后我们为什么需要监听事件?一般有这样一种情况,那就是用于比较两个时间大小或者需要判断在哪个时间点上需要做什么样的操作.基于这样的种种情况,我们很有必要琢磨一下datefield控 ...
- Python学习入门基础教程(learning Python)--5.2 Python读文件基础
上节简单的说明了一下Pyhon下的文件读写基本流程,从本节开始,我们做几个小例子来具体展示一下Python下的文件操作,本节主要是详细讲述Python的文件读操作. 下面举一个例子,例子的功能是读取当 ...
- Codeforces 427 D. Match & Catch
后缀数组.... 在两个串中唯一出现的最小公共子串 D. Match & Catch time limit per test 1 second memory limit per test 51 ...
- 【转】网络中的AS自治域
1. 什么是AS自治域? 全球的互联网被分成很多个AS 自治域,每个国家的运营商.机构.甚至公司等都可以申请AS号码,AS号码是有限的,最大数目是65536.各自分配的IP地址被标清楚属于哪个AS号码 ...
- Codeforces Round #306 (Div. 2) A. Two Substrings 水题
A. Two Substrings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550/pro ...