#include <algorithm>
#include <cstdio>
#include <vector> #define N 10015 using namespace std; vector<int>vec[N];
int n,a,b,m,deep[N],dad[N][]; int LCA(int x,int y)
{
if(deep[x]>deep[y]) swap(x,y);
for(int i=;i>=;i--)
if(deep[dad[y][i]]>=deep[x]) y=dad[y][i];
if(x==y) return x;
for(int i=;i>=;i--)
if(dad[x][i]!=dad[y][i])
x=dad[x][i],y=dad[y][i];
return dad[x][];
} void DFS(int x)
{
deep[x]=deep[dad[x][]]+;
for(int i=;dad[x][i];i++)
dad[x][i+]=dad[dad[x][i]][i];
for(int i=;i<vec[x].size();i++)
if(!deep[vec[x][i]]) dad[vec[x][i]][]=x,DFS(vec[x][i]);
} int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&a,&b);
vec[a].push_back(b);
vec[b].push_back(a);
}
DFS();
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&a,&b);
printf("%d\n",LCA(a,b));
}
return ;
}
/*
6
1 2
1 3
4 2
5 2
3 6
5
4 5
2 6
3 6
1 5
4 6
*/

倍增

 #include <algorithm>
#include <cstdio>
#include <vector> using namespace std; const int N();
vector<int>vec[N];
int n,q,x,y;
int size[N],deep[N],top[N],dad[N]; void DFS(int x)
{
size[x]=; deep[x]=deep[dad[x]]+;
for(int i=;i<vec[x].size();i++)
if(vec[x][i]!=dad[x])
{
dad[vec[x][i]]=x;
DFS(vec[x][i]);
size[x]+=size[vec[x][i]];
}
} void DFS_(int x)
{
int t=; if(!top[x]) top[x]=x;
for(int i=;i<vec[x].size();i++)
if(vec[x][i]!=dad[x]&&size[t]<size[vec[x][i]]) t=vec[x][i];
if(t) top[t]=top[x], DFS_(t);
for(int i=;i<vec[x].size();i++)
if(vec[x][i]!=dad[x]&&t!=vec[x][i]) DFS_(vec[x][i]);
} int LCA(int x,int y)
{
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
x=dad[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
return x;
} int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&x,&y);
vec[x].push_back(y);
vec[y].push_back(x);
}
DFS(); DFS_();
scanf("%d",&q);
for(;q;q--)
{
scanf("%d%d",&x,&y);
printf("%d\n",LCA(x,y));
}
return ;
}

树剖

LCA【模板】的更多相关文章

  1. LCA模板

    /*********--LCA模板--***************/ //设置好静态参数并构建好图的邻接表,然后调用lca_setquery()设置查询 //最后调用lca_start(),在lca ...

  2. 倍增求lca模板

    倍增求lca模板 https://www.luogu.org/problem/show?pid=3379 #include<cstdio> #include<iostream> ...

  3. HDU 2586——How far away ?——————【LCA模板题】

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. 算法复习——LCA模板(POJ1330)

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

  5. hdu 2586 How far away?(LCA模板题+离线tarjan算法)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. LCA模板(数剖实现)

    题目链接:https://www.luogu.org/problemnew/show/P3379 题意:LCA模板题. 思路:今天开始学树剖,先拿lca练练.树剖解lca,两次dfs复杂度均为O(n) ...

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

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

  8. HDU2586 How far away ?(LCA模板题)

    题目链接:传送门 题意: 给定一棵树,求两个点之间的距离. 分析: LCA 的模板题目 ans = dis[u]+dis[v] - 2*dis[lca(u,v)]; 在线算法:详细解说 传送门 代码例 ...

  9. 最近公共祖先(LCA)模板

    以下转自:https://www.cnblogs.com/JVxie/p/4854719.html 首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖 ...

  10. HDU 2586 How far away ?(LCA模板 近期公共祖先啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...

随机推荐

  1. Fitnesse安装

    Fitnesse安装比较简单 1.确保机器上已经安装了java环境

  2. C语言数组和函数实例练习(二)

    1.C语言中不允许函数的嵌套定义,但可以使用函数的嵌套调用. 例1:输入4个整数,找出其中最大的数. #include <stdio.h> #include <stdlib.h> ...

  3. TensorFlow——分布式的TensorFlow运行环境

    当我们在大型的数据集上面进行深度学习的训练时,往往需要大量的运行资源,而且还要花费大量时间才能完成训练. 1.分布式TensorFlow的角色与原理 在分布式的TensorFlow中的角色分配如下: ...

  4. C# 线程知识汇总

    一.基本概念 进程(process)是windows系统中你的一个基本概念,它包含着一个运行程序所需要的资源.一个正在运行的应用程序在操作系统中被视为一个进程,进程可以包括一个或者多个线程.线程是操作 ...

  5. mysql5.7 设置密码

    5.7版本的MYSQL的用户表的密码字段与以往版本有所区别, 不再是password字段 而是authentication_string字段. 请注意修改密码: vim  /opt/mysql_d/m ...

  6. YCbCr to RGB and RGB toYCbCr

    RGB => YCbCr: Y = 0.299R + 0.587G + 0.114BCb = -0.1726R - 0.3388G + 0.5114B + 128Cr = 0.5114R - 0 ...

  7. 修改数组数据头和尾push()、pop()和unshift()、shift()

    1.push().pop()和unshift().shift() 这两组同为对数组的操作,并且会改变数组的本身的长度及内容. 不同的是 push().pop() 是从数组的尾部进行增减,unshift ...

  8. 在django中应用装饰器(一)

    在新写的博客应用中,涉及很多关于权限的问题,比如修改用户信息,博客的修改与删除,虽然默认的提交信息都是session的用户,但是也应该防止一下篡改提交的可能,之前想的是在每个view中加一段判断的逻辑 ...

  9. 以shareExtension为例学习iOS扩展开发

    整体介绍 phone Extension 用法基础详解 share Extension 用法基础详解 demo链接   密码: i72z

  10. vue1 到 vue2 v-el变成ref

    vue1的写法 div class="menu-wrapper" v-el="menu-wrapper"> <ul class="menu ...