POJ 1330 Nearest Common Ancestors 倍增算法的LCA
POJ 1330 Nearest Common Ancestors
题意:最近公共祖先的裸题
思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义。f[i][j]表示i节点的第2j个父亲是多少
这个代码不是我的,转自 邝斌博客
/* ***********************************************
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的更多相关文章
- POJ 1330 Nearest Common Ancestors(Targin求LCA)
传送门 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26612 Ac ...
- POJ 1330 Nearest Common Ancestors (模板题)【LCA】
<题目链接> 题目大意: 给出一棵树,问任意两个点的最近公共祖先的编号. 解题分析:LCA模板题,下面用的是树上倍增求解. #include <iostream> #inclu ...
- POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)
LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...
- 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 / 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 ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- 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 ...
随机推荐
- Mac安装软件时,提示文件已损坏,需要移动到废纸篓的解决方法
1.修改系统偏好设置,安全性与隐私-->将“允许从以下位置下载的应用” ☑️任何来源. 2. mac10.12以上的系统一般没有“任何来源”这个选项,需打开terminnal终端,输入 sudo ...
- 【BZOJ 1193】 [HNOI2006]马步距离
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 原问题可以等价为两个点. 然后其中一个点要移动到另外一个点. 那么我们可以把左下角那个点(对称总是可以得到一个点在左下角)放在原点的 ...
- 兼顾php和jquery新版本的checkbox的写法
<input type="checkbox" class="scate_item" name="scategories[]" valu ...
- jquery easyui的使用
第一步下载jquery easyui 下载地址:http://www.jeasyui.com/download/index.php 第二步创建Java web项目 第三步导入相关的文件..文件夹结构 ...
- 怎么样才算是精通 C++?
C++是一门非常奇妙的语言.让人又爱又恨. 在知乎上看到的一个帖子.怎么样才算是精通C++,这里节选一些精彩的回复. 链接:http://www.zhihu.com/question/20201972 ...
- xcode5. 安装cocos2d-x 学习中。。。
找了一些帖子 没搞出来,后来找到原因了 如今的cocos2d版本号在xcode.5上 没右模版了. 用命令行 来运行.看了官方的文档.最终攻克了--- 对于自己解决的问题都会感到点兴奋. .. ...
- Cannot find the class file for javax.servlet.ServletContext.
当eclipse中新导入的Java Project的时候.往往会碰到各种各样的问题,以下是个典型的问题: Cannot find the class file for javax.servlet.Se ...
- Unity3D的场景单位 和 3D建模软件的单位 之间的关系
转载自 : http://www.ceeger.com/Unity/Doc/2011/3D_to_Unity.html Date:2011-08-24 03:52 Unity的系统单位为米,其他3D软 ...
- blog_html
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html> <html b:v ...
- dns-sd._udp.<domain>. 域名发现 本质和MDNS同
DNS Service Discovery is a way of using standard DNS programming interfaces, servers, and packet for ...