/* ***********************************************
Author :kuangbin
Created Time :2013-9-5 9:45:17
File Name :F:\2013ACM练习\专题学习\LCA\POJ1330_3.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
/*
* POJ 1330
* LCA 在线算法
*/
const int MAXN = ;
const int DEG = ; struct Edge
{
int to,next;
}edge[MAXN*];
int head[MAXN],tot;
void addedge(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void init()
{
tot = ;
memset(head,-,sizeof(head));
}
int fa[MAXN][DEG];//fa[i][j]表示结点i的第2^j个祖先
int deg[MAXN];//深度数组 void BFS(int root)
{
queue<int>que;
deg[root] = ;
fa[root][] = root;
que.push(root);
while(!que.empty())
{
int tmp = que.front();
que.pop();
for(int i = ;i < DEG;i++)
fa[tmp][i] = fa[fa[tmp][i-]][i-];
for(int i = head[tmp]; i != -;i = edge[i].next)
{
int v = edge[i].to;
if(v == fa[tmp][])continue;
deg[v] = deg[tmp] + ;
fa[v][] = tmp;
que.push(v);
} }
}
int LCA(int u,int v)
{
if(deg[u] > deg[v])swap(u,v);
int hu = deg[u], hv = deg[v];
int tu = u, tv = v;
for(int det = hv-hu, i = ; det ;det>>=, i++)
if(det&)
tv = fa[tv][i];
if(tu == tv)return tu;
for(int i = DEG-; i >= ; i--)
{
if(fa[tu][i] == fa[tv][i])
continue;
tu = fa[tu][i];
tv = fa[tv][i];
}
return fa[tu][];
}
bool flag[MAXN];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int n;
int u,v;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
init();
memset(flag,false,sizeof(flag));
for(int i = ;i < n;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
flag[v] = true;
}
int root;
for(int i = ;i <= n;i++)
if(!flag[i])
{
root = i;
break;
}
BFS(root);
scanf("%d%d",&u,&v);
printf("%d\n",LCA(u,v));
}
return ;
}

POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)的更多相关文章

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

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

  2. POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)

    1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST ...

  3. POJ 1330 Nearest Common Ancestors (dfs+ST在线算法)

    详细讲解见:https://blog.csdn.net/liangzhaoyang1/article/details/52549822 zz:https://www.cnblogs.com/kuang ...

  4. POJ 1330 Nearest Common Ancestors LCA题解

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

  5. poj 1330 Nearest Common Ancestors lca 在线rmq

    Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer scienc ...

  6. poj 1330 Nearest Common Ancestors LCA

    题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...

  7. POJ 1330 Nearest Common Ancestors(LCA模板)

    给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...

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

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

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

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

随机推荐

  1. python面向对象(五)之多态

    继承 ​ 在讲多态之前我们再复习下继承,下面是一个例子. ​ Circle 和 Rectangle 继承自 Shape,不同的图形,面积(area)计算方式不同. # shape.py class S ...

  2. node练习笔记

    一.用http模块实现客户端 1.   这个错误的原因是:客户端http_client.js里面的端口和服务端里面的端口不一样 2.querystring.stringify  字符串转换成对象  q ...

  3. 关闭linux退格键和vi发出的嘟嘟声

    以root用户登录,然后请安下面方法进行操作: 在命令行下执行:echo "rmmod pcspkr" >> /etc/rc.d/rc.local

  4. Luogu P1535 【游荡的奶牛】

    搜索不知道为什么没有人写bfs觉得挺像是标准个bfs的 状态因为要统计次数,不能简单地跳过一个被经过的点这样的话,状态量会爆炸采用记忆化设dp[i][j][k]表示在第k分钟到达点(i,j)的方案数以 ...

  5. 怎么使用T-sql生成两位字母

    SQL code select char(cast(rand()*25 as int)+97)+char(cast(rand()*25             as int)+97) select 两 ...

  6. 课堂实验-String类和Arrays类

    课堂实验 在IDEA中以TDD的方式对String类和Arrays类进行学习 测试相关方法的正常,错误和边界情况 String类 charAt split Arrays类 sort binarySea ...

  7. hdu 1028 整数划分 (母函数)

    假如输入44 = 4;4 = 3 + 1;4 = 2 + 2;4 = 2 + 1 + 1;4 = 1 + 1 + 1 + 1;一共5种 假如输入3 用母函数的方法就是写成(1+X+X2+X3)(1+X ...

  8. 【洛谷】P4643 【模板】动态dp

    题解 在冬令营上听到冬眠的东西,现在都是板子了猫锟真的是好毒瘤啊(雾) (立个flag,我去thusc之前要把WC2018T1乱搞过去= =) 好的,我们可以参考猫锟的动态动态dp的课件,然后你发现你 ...

  9. 【AtCoder】ARC095 C-F题解

    我居然每道题都能想出来 虽然不是每道题都能写对,debug了很久/facepalm C - Many Medians 排序后前N/2个数的中位数时排序后第N/2 + 1的数 其余的中位数都是排序后第N ...

  10. NHibernate 错误

    Unable to locate persister for the entity named 'Model.Customer'.The persister define the persistenc ...