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

  1. POJ 1330 Nearest Common Ancestors(Targin求LCA)

    传送门 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26612   Ac ...

  2. POJ 1330 Nearest Common Ancestors (模板题)【LCA】

    <题目链接> 题目大意: 给出一棵树,问任意两个点的最近公共祖先的编号. 解题分析:LCA模板题,下面用的是树上倍增求解. #include <iostream> #inclu ...

  3. POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)

    LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...

  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 / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

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

  6. POJ 1330 Nearest Common Ancestors(lca)

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

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

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

  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. mysql联查中使用if和group by会让你的结果不是你想要的

    mysql中的if语句遇到统计count group by的时候会出现不准确的情况,原因是分组后if条件的结果以第一条为准,不会跟着分组 例如: SELECT t1.*,t2.nick_name,t2 ...

  2. visual studio 2015下python编程的中文字符串问题

    visual studio 2015强大的编程功能,编写起python来也是非常方便的,但其对中文字符的支持不是很好,经常发生莫名其妙的错误,最常见的错误是不报错,也不执行代码. 代码简单如下: x= ...

  3. 2015 Multi-University Training Contest 4 hdu 5335 Walk Out

    Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  4. 通过force index了解的MySQL查询的性能优化

    查询是数据库技术中最常用的操作.查询操作的过程比较简单,首先从客户端发出查询的SQL语句,数据库服务端在接收到由客户端发来的SQL语句后, 执行这条SQL语句,然后将查询到的结果返回给客户端.虽然过程 ...

  5. 基于json数据格式实现的简单数据库——jsonDB

    已在github上建立项目:https://github.com/ThinkerCodeChina/jsonDB /** +-------------------------------------- ...

  6. ZOJ2326Tangled in Cables(最小生成树)

    Tangled in Cables Time Limit: 2 Seconds      Memory Limit: 65536 KB You are the owner of SmallCableC ...

  7. 基于SIP和RTP协议的开源VOIP之QuteCom简单介绍

    **************************************************************************************************** ...

  8. 递归神经网络——就是解决AST这样的问题

    原文:https://zybuluo.com/hanbingtao/note/626300 有时候把句子看做是词的序列是不够的,比如下面这句话『两个外语学院的学生』: 上图显示了这句话的两个不同的语法 ...

  9. MailKit和MimeKit的.NET基础邮件服务

    MailKit和MimeKit的.NET基础邮件服务 邮件服务是一般的系统都会拥有和需要的功能,但是对于.NET项目来说,邮件服务的创建和使用会较为的麻烦..NET对于邮件功能提供了System.Ne ...

  10. Minikube之Win10单机部署Kubernetes(k8s)自动化容器操作的开源平台

    Minikube之Win10单机部署 Kubernetes(k8s)是自动化容器操作的开源平台,基于这个平台,你可以进行容器部署,资源调度和集群扩容等操作.如果你曾经用过Docker部署容器,那么可以 ...