#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的更多相关文章

  1. POJ - 1330 Nearest Common Ancestors(基础LCA)

    POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %l ...

  2. poj 1330 Nearest Common Ancestors 单次LCA/DFS

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19919   Accept ...

  3. POJ 1330 Nearest Common Ancestors(Tarjan离线LCA)

    Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...

  4. POJ.1330 Nearest Common Ancestors (LCA 倍增)

    POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...

  5. POJ 1330 Nearest Common Ancestors 倍增算法的LCA

    POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...

  6. POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

  7. POJ 1330 Nearest Common Ancestors(lca)

    POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...

  8. LCA POJ 1330 Nearest Common Ancestors

    POJ 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24209 ...

  9. POJ 1330 Nearest Common Ancestors 【LCA模板题】

    任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000 ...

随机推荐

  1. 使用sql访问EXECL文件

    --使用sql语句打开访问EXECL文件 --SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDat ...

  2. Use jQuery to hide a DIV when the user clicks outside of it

    http://stackoverflow.com/questions/1403615/use-jquery-to-hide-a-div-when-the-user-clicks-outside-of- ...

  3. 记录一点自己写的Php代码(1)取得任意种类,无限级下线

    //获取所有下线 function get_all_heeler($user_id,$user_rank = 0){ $user_id_array = $user_id; while (true) { ...

  4. java.util.Date转java.sql.Date丢失时间问题

    java.sql.Date 只存储日期数据不存储时间数据// 会丢失时间数据preparedStatement.setDate(1, new java.sql.Date(date.getTime()) ...

  5. JDK - Tomcat - Eclipse - JSP - Servlet 配置运行全攻略

    花了将近两个月的时间,从 JDK 开始一步一步摸索,历经千辛万苦,终于让第一个 Servlet 运行起来了,创建第一个 Servlet  程序确实要比创建第一个 Asp.net 程序困难多了,但是不要 ...

  6. CMSIS Example - osTimer osTimerCreate osTimerStart

    osTimerId timer; uint32_t cnt=; void timerHandler( void * arg ) { cnt++; osTimerStart( timer, ); } o ...

  7. PHP网址

    15个魔术方法的总结: http://blog.csdn.net/bossdarcy/article/details/6210794 PHP代码重构:http://blog.csdn.net/tony ...

  8. 【M5】对定制的“类型转换函数”保持警觉

    1.隐式类型转换有两种情况:单个形参构造方法和隐式类型转换操作符.注意:隐式类型转换不是把A类型的对象a,转化为B类型的对象b,而是使用a对象构造出一个b对象,a对象并没有变化. 2.单个形参构造方法 ...

  9. 2013 Fench Open quart-semifnl press conference

    http://v.youku.com/v_show/id_XNTY3NTAwOTA4.html?firsttime=179 Novak, very  congradutlations,  to rea ...

  10. [AngularJS] Best Practise - Minification and annotation

    Annotation Order: It's considered good practice to dependency inject Angular's providers in before o ...