【知识点:离线算法&在线算法】

一个离线算法,在开始时就需要知道问题的所有输入数据,而且在解决一个问题后就要立即输出结果。

一个在线算法是指它可以以序列化的方式一个个的处理输入,也就是说在开始时并不需要已经知道所有的输入。

【倍增-在线算法】

水题……这道题范围卡得很紧…数组少了一位就会WA。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=+;
const int DEG=;
vector<int> E[MAXN];
int n;
int anc[MAXN][DEG],dep[MAXN]; void addedge(int u,int v)
{
E[u].push_back(v);
} void dfs(int v,int p,int d)
{
anc[v][]=p;
dep[v]=d;
for (int i=;i<E[v].size();i++)
dfs(E[v][i],v,d+);
} void getanc()
{
for (int i=;i<DEG;i++)
for (int j=;j<=n;j++)
{
anc[j][i]=anc[anc[j][i-]][i-];
}
} int swim(int x,int H)
{
for (int i=;H>;i++)
{
if (H&) x=anc[x][i];
H/=;
}
return x;
} int LCA(int u,int v)
{
if (dep[u]<dep[v]) swap(u,v);
u=swim(u,dep[u]-dep[v]);
if (u==v) return u;
for (int i=DEG-;i>=;i--)
{
if (anc[u][i]!=anc[v][i])
{
u=anc[u][i];
v=anc[v][i];
}
}
return anc[u][];
} void init()
{
for (int i=;i<MAXN;i++) vector<int>().swap(E[i]);
scanf("%d",&n);
int notrt[MAXN];
memset(notrt,,sizeof(notrt));
for (int i=;i<n-;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
notrt[v]=;
}
int rt;
for (int i=;i<=n;i++) if (!notrt[i]) //这里注意下标是从1开始的
{
rt=i;
break;
}
dfs(rt,,);
getanc();
} void query()
{
int u,v;
scanf("%d%d",&u,&v);
cout<<LCA(u,v)<<endl;
} int main()
{
int T;
scanf("%d",&T);
for (int kase=;kase<T;kase++)
{
init();
query();
}
return ;
}

【待更新】

【LCA倍增】POJ1330-Nearest Common Ancestors的更多相关文章

  1. POJ1330 Nearest Common Ancestors

      Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24587   Acce ...

  2. LCA POJ 1330 Nearest Common Ancestors

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

  3. POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)

    A - Nearest Common Ancestors Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld &am ...

  4. [POJ1330]Nearest Common Ancestors(LCA, 离线tarjan)

    题目链接:http://poj.org/problem?id=1330 题意就是求一组最近公共祖先,昨晚学了离线tarjan,今天来实现一下. 个人感觉tarjan算法是利用了dfs序和节点深度的关系 ...

  5. POJ1330 Nearest Common Ancestors (JAVA)

    经典LCA操作.. 贴AC代码 import java.lang.reflect.Array; import java.util.*; public class POJ1330 { // 并查集部分 ...

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

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

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

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

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

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

  9. POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)

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

随机推荐

  1. HDU2255:奔小康赚大钱(KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  2. lwIP RAW_API

    lwIP RAW TCP/IP接口 作者: Adam Dunkels, Leon Woestenberg, Christiaan Simons lwIP为使用TCP/IP协议通信的应用程序编程提供了两 ...

  3. 根据select创建input并赋值

    <!DOCTYPE html><html> <head>        <meta charset="UTF-8">         ...

  4. Spring学习--泛型依赖注入

    暂时没有搞懂.

  5. 通过 CLI 搭建 ghost

    参考: ghost 官网 系统架构说明 架构 架构说明 本实践将 web 接入, nodejs 服务, 数据库分离, 适合生产环境场景. nginx 接入请求, 反向代理后端 nodejs 服务 no ...

  6. winds dlib人脸检测与识别库

    在人脸检测与人脸识别库中dlib库所谓是非常好的了.检测效果非常ok,下面我们来了解一下这个神奇的库吧! 第一步我们首先学会安装:dlib ,winds+pytho3.6.5  Windows不支持p ...

  7. python自带的线程池和进程池

    #python自带的线程池 from multiprocessing.pool import ThreadPool #注意ThreadPool不在threading模块下 from multiproc ...

  8. 视频图像处理基础知识5(RGB与Ycbcr相互转换公式 )【转】

    转自:http://blog.csdn.net/Times_poem/article/details/51471438 版权声明:本文为博主原创文章,未经博主允许不得转载. 需求说明:视频处理算法基本 ...

  9. linux设备驱动中的并发控制【转】

    转自:http://www.cnblogs.com/plinx/archive/2013/01/28/2873121.html 在linux内核中,主要的静态发生于以下几种情况: 1.对称多处理器(S ...

  10. (十四)git操作

    https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000